Cell to String Conversion

5 vues (au cours des 30 derniers jours)
Jay
Jay le 2 Oct 2014
Modifié(e) : Win co le 2 Oct 2014
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.

Réponse acceptée

Stephen23
Stephen23 le 2 Oct 2014
Modifié(e) : Stephen23 le 2 Oct 2014
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?

Plus de réponses (1)

Win co
Win co le 2 Oct 2014
Modifié(e) : Win co le 2 Oct 2014
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};

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by