Saving cells created using mat2cell
조회 수: 1 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Geoff Hayes
2014년 9월 3일
편집: Geoff Hayes
2014년 9월 3일
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!
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!