Overwrite empty cell array with strings

조회 수: 24(최근 30일)
Mat
Mat 2014년 11월 4일
댓글: Mat 2014년 11월 4일
I have an empty cell array, I want to add a row of strings to this matrix.
The empty matrix is 12x100. The row of strings can vary in length, but will always be below 100. Hence I want an oversized cell array to accommodate larger rows of strings. I want to put the row of strings into a specified row within the empty matrix, in my example code, I'm trying to put the row of stings into the first 41 columns in row 2....
I don't understand why the below code doesn't work:
MasterCellArray{2,1:41}=rowofstrings
or
MasterCellArray{2,1:41}=rowofstrings(1,41)
Any help is appreciated
  댓글 수: 2
Mat
Mat 2014년 11월 4일
rowofstrings is a row of strings, they follow the form CLPB44444, where the numbers are variable. Eg. CLPB44445 CLPB44453 CLPB44320... I have 12 batches of data like this, with a varying number of strings in each batch. Hence why I wanted 12 rows. I wanted a 100 columns to ensure enough space to fit all of the data.

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

채택된 답변

Matt Tearle
Matt Tearle 2014년 11월 4일
If rowofstrings is a 1-by-41 cell array then your first line will work if you change the {} to ():
MasterCellArray(2,1:41) = rowofstrings
If not, then you'll have a problem... Using the {} on the left is going to fail because that returns 41 individual elements. If you use (), you're indexing into a 1-by-41 cell array that is a portion of MasterCellArray. That means the expression on the right of the = must also be a 1-by-41 cell array. So if rowofstrings is a (1-by-41) char array, for example, you could do something like:
MasterCellArray(2,1:41) = cellstr(rowofstrings')'
(This converts rowofstrings to a cell array, letter-by-letter, then assigns each cell to the appropriate element of MasterCellArray.)
  댓글 수: 1
Mat
Mat 2014년 11월 4일
Thanks Matt, first solution worked - I thought I had tried changing the brackets already but I expect there were other problems with my code when I tried that, and never went back. I had been trying to convert my rowofstrings to fit into a zeroes matrix, but I think this was impossible because the rows contain letters (eg CLPB1234). On a learning curve here, so thanks for the talk through!

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by