Unnest a Matrix of strings

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
Andrew Newell el 25 de Mzo. de 2015
Editada: Andrew Newell el 25 de Mzo. de 2015

1 voto

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 el 25 de Mzo. de 2015
Editada: Ced el 25 de Mzo. de 2015

0 votos

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
Brian Morrison el 25 de Mzo. de 2015
I appreciate the quick responses.
I apologize if I wasn't clear enough. (first time poster) I simplified the problem to ask a more concise question.
What I am really trying to do is reorganize a large file for work. This can probably be done in excel but I am not at all familiar at excel as I am with Matlab.
I am starting with about 8000 lines like so:
'abc, defg, hijkl'
'mnop, qrs, tuv'
'wxyz, ab, cdef'
. . .
I want to end up with a 3 column matrix (or cell, no sure what the correct terminology is)(if it is possible):
M=
{'abc' defg' hijkl'
'mnop' 'qrs' 'tuv'
'wxyz' 'ab' 'cdef'
...}
I know how to "break up" each line with something like a strsplit function and then I can even group 'abc,'mnop', and 'wxyz'. But ran into the issue where I got an output like the matrix below when I tried to regroup each column of data to form one matrix instead of three separate arrays:
M= {'abc' defg' [1x22 char]
'mnop' 'qrs' [1x21 char]
'wxyz' 'ab' [1x29 char]
...}
any further pointers would be greatly appreciated.
Andrew Newell
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
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
Brian Morrison el 25 de Mzo. de 2015
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 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.

Iniciar sesión para comentar.

Konstantinos Sofos
Konstantinos Sofos el 25 de Mzo. de 2015

0 votos

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,

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 25 de Mzo. de 2015

Editada:

el 25 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by