Matlab: Export concatenation of cell arrays into a txt file

2 views (last 30 days)
I have two different sets of arrays within an array, which I called:
y={...}; % cell array of 1x1, with a 26x1 cell array within it.
Only_Data={...} %cell array of 525x1, with 525 different 1x12 cell arrays
To have a better idea of what's inside of them, you can look these two photos:
The data that's within the arrays is string. I want to concatenate these two arrays into one and print that final result into a .txt file. But I'm having problems doing the last part.
This is what I've done for that purpuse:
new_subject3=[y; Only_Data];
[nrows,ncols] = size(new_subject3);
for row = 1:nrows
fprintf(fid ,'%s\n', new_subject3{row}{1:end});
For some reason I'm not aware of, it only prints the 'y' array, but doesn't print anything from the 'Only_Data' array. Why would that be? I'm clueless. I'm sure it's not about the type of data, but further than that I don't know.
I'll be glad to learn how to do this, thanks in advance.
Stephen23 on 10 Aug 2016
@Ajpaezm: I moved your screen shots onto this forum. Next time please upload them yourself using the buttons above the textbox.

Sign in to comment.

Accepted Answer

Stephen23 on 10 Aug 2016
Edited: Stephen23 on 10 Aug 2016
The strings are nested in a different number of cell arrays. The string in Only_Data are nested in three layers of cell arrays, whereas y strings are only in two layers of cell arrays.
So your data have different levels of nesting.
Try these:
class(OnlyData{1}{1}{1}) %etc, until you get to a string
How do we know this? The clue is in the quotation marks! MATLAB only displays the quotation marks when the string is inside a cell array. So your screenshot for OnlyData{1,1} show quotation marks, whereas y{1,1} does not.
It is easy to confirm too:
>> A = {{'A'}};
>> B = {{{'B'}}};
Your idea of how to merge them was fine, you just made the mistake of relying on what you believe that data to be like, rather than actually checking what is really there (and is probably explained in the documentation of whatever generated that data in the first place).
Stephen23 on 10 Aug 2016
Edited: Stephen23 on 10 Aug 2016
Actually I would experiment with cell2mat, and first reduce the nesting down to just one cell array before applying this to fprintf, and not bothering to join them together:
yNew = y{1};
dNew = cell2mat(cell2mat(OnlyData));
This is untested, but it should get you started. If you get the orientation correct you won't even need the loop.
Ajpaezm on 10 Aug 2016
It worked nicely, I can print values.
Now, the format it's a bit messed up, but that's something for another question.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!