MATLAB Answers

Matlab: Export concatenation of cell arrays into a txt file

7 views (last 30 days)
Ajpaezm
Ajpaezm on 10 Aug 2016
Commented: Ajpaezm on 10 Aug 2016
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];
new_subject3;
outfile='mynewfile';
filename=[outfile,'.txt'];
fid=fopen(filename,'w');
[nrows,ncols] = size(new_subject3);
for row = 1:nrows
fprintf(fid ,'%s\n', new_subject3{row}{1:end});
end
fclose(fid);
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.

  2 Comments

Stephen Cobeldick
Stephen Cobeldick 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

Stephen Cobeldick
Stephen Cobeldick on 10 Aug 2016
Edited: Stephen Cobeldick 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(y{1}{1})
class(OnlyData{1}{1})
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).

  3 Comments

Ajpaezm
Ajpaezm on 10 Aug 2016
class(y{1}{1}) returns 'char' class(OnlyData{1}{1}) returns 'cell' (Can I use the command char on this?) class(OnlyData{1}{1}{1}) returns 'char'
Stephen Cobeldick
Stephen Cobeldick 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));
fprintf(...,yNew{:})
fprintf(...,dNew{:})
This is untested, but it should get you started. If you get the orientation correct you won't even need the loop.
Ajpaezm
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)

Sign in to answer this question.


Translated by