Convert a 3d matrix mat file to multiples 2d CSV files.

3 次查看(过去 30 天)
Hello all.
I am an iniciant in MatLab. Could you teach me how to convert a 3d mat file table (m x n x p) to multiples p CSV files (each representing a 2d table m x n)?
I need to get p files each containing m x n values.
Thank you very much.
  1 个评论
dpb
dpb 2017-3-3
"I need to get p files each containing m x n values."
Why? The power in Matlab is using the vector notation so why not use the data as is in a much more compact and faster form than creating a bunch of separate text files?
What can you do with those that can't do from the .mat file?

请先登录,再进行评论。

回答(2 个)

dpb
dpb 2017-3-3
编辑:dpb 2017-3-5
Against my better judgement, but Walter says I preach too much... :)
Presume array is x in memory--
for i=1:size(x,3)
fname=[fulfile('yourpath',['yourbasename' num2str(i,'%4d')] '.dat'] % build a file name
fid=fopen(fname,'w'); % open file handle with it
fwrite(x(:,:,i) % write as unformatted
fid=fclose(fid); % close this one...
end
If are still adamant about formatted, replace fopen|fwrite combination with
csvwrite(fname,x(:,:,i))
and watch the size go up and speed go down...

GEEVARGHESE TITUS
If you want to just access the p, mxn structures you can apply the script like
b1=a(:,:,1);
...
bp=a(:,:,p);
There is no point in such assignments, as they just use up memory. Just say you have a matrix a of size 4x3x2 and you want to find the sum of each of the 4x3 structures, you can just apply the script
for i=1:size(a,3)
b(i)=sum(sum(a(:,:,i)));
end
  2 个评论
Mauro Larrat
Mauro Larrat 2017-3-3
编辑:Mauro Larrat 2017-3-3
I would appreaciate hearing your thoughts on this, but I would like to save the p matrices (m x n) in separated files. It is 4096x5x1700 in a mat file. I want to get 1700 (or p) files of 4096x5.
Thanks,
dpb
dpb 2017-3-3
编辑:dpb 2017-3-3
I again ask "Why!!!???" would you want to have to fool with 1700 files(!) instead of just dealing with the planes of the 3D array? After you do that, then you'll be back wanting to know how to process 1700 files in a loop, in all likelihood.
Now, granted, that's beginning to get to be a sizable array if you only need one slice to do something complex on but it's still "only"
>> 4096*5*1700*8/1024/1024
ans =
265.6250
>>
MB which with today's memory availability is pretty manageable.
Again, what's going to be done with the files once you've created them--you surely can't visually inspect each of them and the size will balloon by 10X or greater when you convert to text so if you're adamant going to split them out, at least keep them as .mat files or unformatted.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by