Unnest a Matrix of strings

5 views (last 30 days)
Brian Morrison
Brian Morrison on 25 Mar 2015
Edited: Andrew Newell on 25 Mar 2015
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-

Answers (3)

Andrew Newell
Andrew Newell on 25 Mar 2015
Edited: Andrew Newell on 25 Mar 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.

Ced
Ced on 25 Mar 2015
Edited: Ced on 25 Mar 2015
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 Comments
Brian Morrison
Brian Morrison on 25 Mar 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 on 25 Mar 2015
I think I understand what you want now, and my second edit in my own answer will do it.

Sign in to comment.


Konstantinos Sofos
Konstantinos Sofos on 25 Mar 2015
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,

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!