필터 지우기
필터 지우기

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

조회 수: 10 (최근 30일)
Mauro Larrat
Mauro Larrat 2017년 3월 3일
편집: dpb 2017년 3월 5일
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
GEEVARGHESE TITUS 2017년 3월 3일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by