Cell to String Conversion

8 次查看(过去 30 天)
Jay
Jay 2014-10-2
编辑: Win co 2014-10-2
I have created a cell from another cell with a 1,n dimension.
I would like to convert the values in the 1,n cell to a matrix of strings for a following if statement.
Is there a simple function for this conversion similar to cell2mat?
If not, what is the easiest way of achieving this conversion?
I don't want to specify the values in the cell manually, but rather have the code transcribe it, this would cater for dynamic cell values.

采纳的回答

Stephen23
Stephen23 2014-10-2
编辑:Stephen23 2014-10-2
You do not tell us what type/class the data are in your cell array, and also do not give us any indication of their size, but simply write "I would like to convert the values in the 1,n cell to a matrix of strings". If we assume that the "values" are numeric arrays, then you will need to apply some function to convert them to strings:
str = num2str(num)
will do this, for example (you need to find the function that suits your purpose). As your numeric arrays are contained in a cell array, you will need to access the numeric arrays in each cell and apply the function to it. This can be done:
A = {num1,num2,...};
B = cellfun(@num2str,A, 'UniformOutput',false);
  • or in a loop:
B = cell(size(A));
for k = 1:numel(A)
B{k} = num2str(A{k});
end
This statement is very interesting: "I would like to convert the values ... to a matrix of strings for a following if statement". If you need to compare values for an if statement, why convert them to strings?

更多回答(1 个)

Win co
Win co 2014-10-2
编辑:Win co 2014-10-2
Hi, conversion cell to string is automatic. Eg: given a following cell X:
{1,1} -> [1 2 3]
{1,2} -> [aa bb cc]
now extract the 2nd element of X:
s=X{2};
s is now a string cell 1x3
now you can do a "for" loop to get string value of each element of the last cell like that:
x=s{i};

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by