필터 지우기
필터 지우기

Unnest a Matrix of strings

조회 수: 2 (최근 30일)
Brian Morrison
Brian Morrison 2015년 3월 25일
편집: Andrew Newell 2015년 3월 25일
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-

답변 (3개)

Andrew Newell
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.

Ced
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
Brian Morrison
Brian Morrison 2015년 3월 25일
They have the quotes.
When I say "regroup" I mean I took 'abc', 'mnop', 'wxyz', and made that a 1x3 matirx and called it "A".
Then I took 'defg', 'qrs', and 'ab', made that a 1x3 matrix and called it "B"
Then I took 'hijkl', 'tuv', and 'cdef' made that a 1x3 matrix and called it "C"
I then wrote M=[A,B,C] and got the result
M= 'abc' 'defg' [1x22 char]
'mnop' 'qrs' [1x21 char]
'wxyz' 'ab' [1x29 char]
Andrew Newell
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
Konstantinos Sofos 2015년 3월 25일
Hi,
You could use simple the vertcat
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,

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by