Matlab: Export concatenation of cell arrays into a txt file

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2016-8-10
@Ajpaezm: I moved your screen shots onto this forum. Next time please upload them yourself using the buttons above the textbox.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2016-8-10
编辑:Stephen23 2016-8-10
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 个评论
Stephen23
Stephen23 2016-8-10
编辑:Stephen23 2016-8-10
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 2016-8-10
It worked nicely, I can print values.
Now, the format it's a bit messed up, but that's something for another question.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by