How to put a string in a cell array ?

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 个评论

Is UJourney a cell array that contains cell arrays? And you want to set the first 5 cells of UJourney{1,1} to all be the string 'accepted' ??
nope i want to add in the 5th colums in Ujourney{1,1} the string 'accepted'
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.
well , its not problem if i used multiple columns each row to put 'accepted'
here's an example of input and then output i wanna see
Ujourney{1,1}
-0.029 0.634 1 5
-0.251 0.130 1 8
0.046 0.724 1 5
Ujourney{1,1}(:,5:6)='accepted'
-0.029 0.634 1 5 accepted
-0.251 0.130 1 8 accepted
0.046 0.724 1 5 accepted

请先登录,再进行评论。

 采纳的回答

T = Ujourney{1,1};
if ~iscell(T)
T = num2cell(T);
end
T(:,5) = {'accepted'};
Ujourney{1,1} = T;

更多回答(2 个)

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 个评论

Note: Walter's and my code give different things because of the lack of clarity in your description of what you want.
Yea I'm sorry for that , I appreciate your attention .

请先登录,再进行评论。

Rahul Prajesh
Rahul Prajesh 2017-8-4
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 个评论

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.

请先登录,再进行评论。

类别

帮助中心File 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