Unnest a Matrix of strings
Mostrar comentarios más antiguos
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-
Respuestas (3)
Andrew Newell
el 25 de Mzo. de 2015
Editada: Andrew Newell
el 25 de Mzo. de 2015
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.
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 comentarios
Brian Morrison
el 25 de Mzo. de 2015
Andrew Newell
el 25 de Mzo. de 2015
I'm not sure what you mean by regrouping each column of data. What command did you use and what do you want to see in the final version of M?
Andrew Newell
el 25 de Mzo. de 2015
Also, do the strings in your file actually have quotes around them, or do they look like this?
abc, defg, hijkl
Brian Morrison
el 25 de Mzo. de 2015
Andrew Newell
el 25 de Mzo. de 2015
I think I understand what you want now, and my second edit in my own answer will do it.
Konstantinos Sofos
el 25 de Mzo. de 2015
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,
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!