Matlab: Export concatenation of cell arrays into a txt file

2 visualizaciones (últimos 30 días)
Ajpaezm
Ajpaezm el 10 de Ag. de 2016
Comentada: Ajpaezm el 10 de Ag. de 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 comentarios
Stephen23
Stephen23 el 10 de Ag. de 2016
@Ajpaezm: I moved your screen shots onto this forum. Next time please upload them yourself using the buttons above the textbox.
Ajpaezm
Ajpaezm el 10 de Ag. de 2016
Thank you Stephen.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 10 de Ag. de 2016
Editada: Stephen23 el 10 de Ag. de 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 comentarios
Stephen23
Stephen23 el 10 de Ag. de 2016
Editada: Stephen23 el 10 de Ag. de 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 el 10 de Ag. de 2016
It worked nicely, I can print values.
Now, the format it's a bit messed up, but that's something for another question.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by