Cell to String Conversion

조회 수: 45(최근 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.

채택된 답변

Stephen
Stephen 2014년 10월 2일
편집: Stephen 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};

Community Treasure Hunt

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

Start Hunting!

Translated by