Write the content of individual cells in a cell array to discrete text files in a specified directory

2 ビュー (過去 30 日間)
Hi
I have an nx1 cell array, M, containing 4x4 matrices in individual cells. For example:
M{1} =
0.9990 -0.0007 0.0010 0.0013
-0.0007 0.9999 -0.0001 0.0509
0.0010 -0.0001 0.9996 -0.0343
0 0 0 1.0000
I need to write the contents of each individual cell to a discrete text file with sequential names and a .dat extension (e.g. cam1.dat, cam2.dat etc) to a specified folder created previously:
mkdir('C:\transform', 'cameras1');
I can make a single file in the desired format using:
filename = 'C:\transform\cameras1\cams.dat';
fid = fopen(filename,'w');
fprintf(fid,'%-.8e %-.8e %-.8e %-.8e\r\n', transpose(M{1}));
fclose(fid);
But am unsure how to excecute this code iteratively for the entire contents of M.
Any help would be greatly appreciated.
Thomas

採用された回答

Thomas Seers
Thomas Seers 2012 年 12 月 20 日
Thanks Jan
I have managed to get the code working using:
for k=1:size(M,1)
filename = ['C:\transform\cameras1\' 'cams' num2str(k) '.dat'];
fid = fopen(filename,'w');
fprintf(fid,'%-.8e %-.8e %-.8e %-.8e\r\n', transpose(M{k}));
fclose(fid);
end
The FAQ document was most useful. I will try to refer to it first in the future. I'm not sure of the etiquatte for accepting answers in this instance (feel free to advise): thank you nevertheless.
p.s. my 3 year old likes your avatar!

その他の回答 (1 件)

Jan
Jan 2012 年 12 月 20 日
  1 件のコメント
Jan
Jan 2012 年 12 月 20 日
Dear Walter, I've explained 1 minute ago, that I do not post links to the FAQ anymore. But every rule has to be broken from time to time.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by