How to build a .dat file with a cell variable

There is a picture in my textbook
Since this,I try to create a same .dat file for experiement.
>> m={'x2.3y4.56','x7.7y11.11','x12.5y5.5'};
>> save('file.dat','m')
>> type file.dat
MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Sat Jul 08 11:19:36 2017
As you see,the type will give a wrong result.So I try
save('file.dat','m','-ascii')
Well,this time I will get some wrong information.How to create that *.dat* file?

1 件のコメント

倪清策 倪
倪清策 倪 2021 年 6 月 1 日
[R,P]=corrcoef(test);
R=[2.41;2.05;2.34;1.48;1.39];
P=[0.34;0,5;0.42;0.43;0.42];

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

回答 (1 件)

Image Analyst
Image Analyst 2017 年 7 月 8 日

0 投票

Use fprintf():
m = {'x2.3y4.56', 'x7.7y11.11', 'x12.5y5.5'};
fid = fopen('file.dat', 'wt');
for k = 1 : length(m)
fprintf('%s\n', m{k});
end
fclose(fid);
type file.dat

7 件のコメント

Yode
Yode 2017 年 7 月 8 日
That work for you?When I run
type file.dat
I get a empty output..
Walter Roberson
Walter Roberson 2017 年 7 月 8 日
編集済み: Walter Roberson 2017 年 7 月 8 日
m = {'x2.3y4.56', 'x7.7y11.11', 'x12.5y5.5'};
fid = fopen('file.dat', 'wt');
for k = 1 : length(m)
fprintf(fid, '%s\n', m{k});
end
fclose(fid);
type file.dat
Image Analyst
Image Analyst 2017 年 7 月 8 日
Sorry, I was testing it in the command window first and forgot to put the "fid," in the fprintf() when I posted it. Walter corrected it.
Yode
Yode 2017 年 7 月 12 日
@Walter Roberson I know the "t" in 'wt' can be omit here?
Image Analyst
Image Analyst 2017 年 7 月 12 日
Try it and see if you like/accept any differences.
Walter Roberson
Walter Roberson 2017 年 7 月 12 日
The difference between 'w' and 'wt' is that on MS Windows systems, if you use 'wt' then any time that you send a newline (char(10), format code '\n') to the file, the I/O subsystem will automatically write carriage return (char(13), format code '\r') before the newline. This provides backwards compatibility with NotePad and other old programs that expect CR/NL pairs at the end of lines.
On Linux and OS-X, 'w' and 'wt' do the same thing, which is to say that they send only newline without carriage return.
Jan
Jan 2017 年 7 月 12 日
In addition to the line break conversion, the output in text mode considers char(8) or sprintf('\b') as backsapce and removes the former character. The EOF-Escape sequence can stop the reading of the file also, even if more characters are following. In consequence the number of written characters must not equal the number of characters to write.

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

カテゴリ

ヘルプ センター および File Exchange环境和设置 についてさらに検索

タグ

質問済み:

2017 年 7 月 8 日

コメント済み:

2021 年 6 月 1 日

Community Treasure Hunt

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

Start Hunting!