Saving cells created using mat2cell

1 Ansicht (letzte 30 Tage)
Julia
Julia am 3 Sep. 2014
Beantwortet: Julia am 3 Sep. 2014
I break a large array called tdm into a set of smaller cells of dimensions Dx, Dy, Dz using the mat2cell command
cutPre = mat2cell (tdmPre, [Dx], [Dy], [Dz])
I can call these cells like this: cutPre {1,1,1}, cutPre {1,1,2} ... until cutPre {4,4,5}
I would like to save these cells as a .h5-file using the same numbers in the name (e.g. cutPre(1,1,1).h5) and tried the following loop to do that:
for i= 1:nx;
for j= 1:ny;
for k= 1:nz;
assignin('base', ['cutPre' num2str(i,j,k)], cutPre{i,j,k});
hdf5write('cutPre',num2str(i,j,k),'.h5','/cutPre{i,j,k}','cutPre',num2str(i,j,k));
end;
end;
end;
The assign command does not work for several input arguments. Any suggestions?

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 3 Sep. 2014
Bearbeitet: Geoff Hayes am 3 Sep. 2014
Julia - if I run a subset of your code, and in particular, the line
assignin('base', ['cutPre' num2str(i,j,k)], cutPre{i,j,k});
it isn't the assignin that is causing a problem, but num2str
Error using num2str
Too many input arguments.
because only one input is allowed for this function. Use sprintf instead of num2str to format a string with numeric data. Something like
assignin('base', ['cutPre' sprintf('%d_%d_%d',i,j,k)], cutPre{i,j,k});
The above will create a variable with the name of cutPre1_1_1 (for the first iteration) but this variable will be created in the base workspace, so that outside of this function. You could use the eval command to create this variable within the scope of the current function, but I don't see that as necessary. (And you should try to avoid using eval because it makes it difficult to debug the code if something goes wrong.)
Instead, since you want to save these cells as a .h5-file using the same numbers in the name (e.g. cutPre(1,1,1).h5), then just use the sprintf to create the file name, for example, cutPre1_1_1.h5 as
filename = ['cutPre' sprintf('%d_%d_%d',i,j,k) '.h5'];
and then write the data to file with
hdf5write(filename,sprintf('/cutPre{%d,%d,%d}',i,j,k),cutPre{i,j,k});
I'm not all that clear on the second input parameter, location, especially as only one piece of data is written to each file, but the rest should be fine.
Try the above and see what happens!

Weitere Antworten (1)

Julia
Julia am 3 Sep. 2014
Thanks Geoff, its working!

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by