Unnest a Matrix of strings
조회 수: 2 (최근 30일)
이전 댓글 표시
I have an older version of matlab (R2008a) and I am trying to Unnest an output from a for loop where the out put should be a 3x3 matrix of strings. Where the output currently displayed is:
M
= {3x1 cell} {3x1 cell} {3x1 cell}
I have tried:
[L{:}]
But this returns a matrix where the cells in the third column are still displayed as:
[1x22 char]
[1x21 char]
[1x29 char]
any help would be appreciated thanks
B-
댓글 수: 0
답변 (3개)
Andrew Newell
2015년 3월 25일
편집: Andrew Newell
2015년 3월 25일
If I understand what you're trying to do, you can't do it. A 3x3 matrix of strings looks like this:
['abc';'def';'ghi']
ans =
abc
def
ghi
If you try something like repmat(ans,[1 3]), you get:
ans =
abcabcabc
defdefdef
ghighighi
If you want a string array, you could try
char([L{:}])
This will give you an array of 9 strings, one per line.
EDIT: Based on the discussion below, you might want to try:
fileID = fopen(your_file_name,'r');
C = textscan(fileID,'%s %s %s\n','Delimiter',',');
fclose(fileID)
EDIT2: Based on further discussion, I think this will do what you want:
fileID = fopen('string_data','r');
C = textscan(fileID,'%s %s %s\n','Delimiter',',','CollectOutput',true);
fclose(fileID);
C = regexprep(C{1},'''','')
The textscan command divides the strings up, ignoring the commas, and collects the result in a cell array C inside of which is an N x 3 cell array (don't you just love MATLAB I/O?). The regexprep command gets rid of stray quotation marks.
댓글 수: 0
Ced
2015년 3월 25일
편집: Ced
2015년 3월 25일
I'm afraid that this is a bit unavoidable.
L = [M{:}];
would return you a 3x3 cell with the desired strings. If you convert that into a matrix though (e.g. with cell2mat), you run into two problems:
a) [ 'ab','bb','cb' ] is interpreted by matlab as a concatenation of characters, and not three distinct entries of a matrix.
b) if I understand correctly, your entries do not all have the same number of characters. Because matlab interprets each character of a string as a "column" in a matrix, you cannot save strings of different lengths in the same matrix.
Personally, I would go with a 3x3 cell. I don't see a reason not to use cells for non-numerical values.
EDIT: too slow... :)
댓글 수: 5
Andrew Newell
2015년 3월 25일
I think I understand what you want now, and my second edit in my own answer will do it.
Konstantinos Sofos
2015년 3월 25일
Hi,
vertcat(M{:})
Example:
>> M = {{'a','b','c'},{'d','e','f'},{'g','h','I'}}
M =
{1x3 cell} {1x3 cell} {1x3 cell}
>> L = vertcat(M{:})
L =
'a' 'b' 'c'
'd' 'e' 'f'
'g' 'h' 'I'
Regards,
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!