How to put a string in a cell array ?

조회 수: 22 (최근 30일)
Bret
Bret 2012년 6월 16일
댓글: Walter Roberson 2017년 8월 4일
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
Walter Roberson
Walter Roberson 2012년 6월 16일
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.
Bret
Bret 2012년 6월 17일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 17일
T = Ujourney{1,1};
if ~iscell(T)
T = num2cell(T);
end
T(:,5) = {'accepted'};
Ujourney{1,1} = T;

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 6월 17일
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
Image Analyst
Image Analyst 2012년 6월 17일
Note: Walter's and my code give different things because of the lack of clarity in your description of what you want.
Bret
Bret 2012년 6월 17일
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
Walter Roberson
Walter Roberson 2017년 8월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

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