How to put a string in a cell array ?
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Bret
el 16 de Jun. de 2012
Comentada: Walter Roberson
el 4 de Ag. de 2017
well, i ave a cell called UJourney{1,1} . and i want to put in UJourney{1,1}(:,5) string called 'accept' or 'reject'.
UJourney{1,1} (:,5)={'accepted'} this is wat i m doing but its not working , thanks for attention .
4 comentarios
Walter Roberson
el 16 de Jun. de 2012
What data type is Ujourney{1,1} expected to be? The only data type that you can store strings in is cell arrays. If Ujourney{1,1} is expected to be a character array then you cannot put a string into a particular column of a character array: strings are row vectors, not column vectors. Are you trying to put 'accepted' into multiple rows of a character array, starting at column 5 of each row? If so then do you want to overwrite the existing characters in column 6 onward or do you want the characters in column 6 onward to be "pushed over" to the right?
Perhaps it would be easier if you gave an example UJourney{1,1} input and desired output.
Respuesta aceptada
Walter Roberson
el 17 de Jun. de 2012
T = Ujourney{1,1};
if ~iscell(T)
T = num2cell(T);
end
T(:,5) = {'accepted'};
Ujourney{1,1} = T;
0 comentarios
Más respuestas (2)
Image Analyst
el 17 de Jun. de 2012
Try this:
% Initialize.
% Stuff a 3 by 4 array of doubles
% into the upper left cell of the cell array.
Ujourney{1,1} = [...
-0.029 0.634 1 5
-0.251 0.130 1 8
0.046 0.724 1 5 ]
% Now begin...
% Extract the double array from that cell
% into a regular numerical matrix.
dblMatrix = Ujourney{1,1}
% Get the number of rows and columns.
[rows columns] = size(dblMatrix)
% Convert the numbers into a character array.
charMatrix = [];
for row = 1 : rows
% First stick the numbers in.
thisRowString = sprintf('%10.4f ', dblMatrix(row,:));
% Now add ' accepted'
charMatrix = [charMatrix; sprintf('%s accepted', thisRowString)]
end
% Now replace the Ujourney{1,1} cell,
% which currently contains a double array,
% with a cell that contains our 2D character array we just created.
Ujourney{1,1} = charMatrix
2 comentarios
Image Analyst
el 17 de Jun. de 2012
Note: Walter's and my code give different things because of the lack of clarity in your description of what you want.
Rahul Prajesh
el 4 de Ag. de 2017
Its an old question, still i think, I should add my answer to it...
[row,col] = size(str_cell);
str_cell{row,col+1} = string_to_be_added;
1 comentario
Walter Roberson
el 4 de Ag. de 2017
That could be written as
str_cell{end,end+1} = string_to_be_added;
However, your code does not add the string to every row as required by the original question. Your code also requires that str_cell be what is called Ujourney{1,1} in the original question, and your code does not then update Ujourney afterwards. Your code also requires that str_cell, which is to say Ujourney{1,1} be a cell array, which is not the case in the original question: in the original question, Ujourney{1,1} was a numeric array.
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!