writing cellarray data into text file with specific tab space and precision.

1 回表示 (過去 30 日間)
sermet
sermet 2015 年 4 月 25 日
編集済み: Stephen23 2015 年 4 月 25 日
cellArray =
'p.1' [38.141794059793398] [31.983184186130529] [1.751451095529832e+03]
[ 10] [38.094455446389709] [31.940682658186898] [1.779530219110660e+03]
'ab1' [38.162956465159276] [32.019563510560964] [1.542875077314675e+03]
[ 20] [38.176654734516106] [32.068817577008474] [1.389497182917781e+03]
Name Size Bytes Class Attributes
cellArray 4x4 1916 cell global
% I need to write cellArray into text file as 3 tab space after the first column and 2 tab space between the numeric numbers. I need to get 8 numbers after the dot for numeric data.
% Thanks in advance.
  1 件のコメント
Jan
Jan 2015 年 4 月 25 日
What exactly are "3 tab spaces"? Three spaces or three tab characters?

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

採用された回答

Jan
Jan 2015 年 4 月 25 日
C = {'p.1', 38.141794059793398, 31.983184186130529, 1.751451095529832e+03; ...
10, 38.094455446389709, 31.940682658186898, 1.779530219110660e+03; ...
'ab1', 38.162956465159276, 32.019563510560964, 1.542875077314675e+03; ...
20, 38.176654734516106, 32.068817577008474, 1.389497182917781e+03}
% It took me time to edit the data in a clean syntax. Please do this by your own.
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file for writing: %s', FileName);
Ct = C.';
fprintf(fid, '%s %.8f %.8f %.8f\n%f %.8f %.8f %.8f\n', Ct{:});
fclose(fid)
Or with \t instead of teh spaces depending on what "tab spaces" means.
  4 件のコメント
sermet
sermet 2015 年 4 月 25 日
I'm sorry Simon. I don't know well about fprintf. But the result is still same when I replace '%.8f' by '%d': like; fprintf(fid, '%s %.8f %.8f %.8f\n%f %d %.8f %.8f\n', Ct{:});
Stephen23
Stephen23 2015 年 4 月 25 日
編集済み: Stephen23 2015 年 4 月 25 日
@sermet: considering this and all of your other questions related to fprintf precision:
now would be a good time to read the documentation:
The lovely people at TMW spent a lots of time writing it, and it contains lots of useful information!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by