Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Saving as a .txt file help

2 ビュー (過去 30 日間)
Huseyin
Huseyin 2014 年 3 月 3 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have a script like:
sample(1,:)='a ';
sample(2,:)='b ';
sample(3,:)='c ';
sample(4,:)='ab';
sample(5,:)='bc';
fid=fopen('MyFile.txt','w');
fprintf(fid,sample);
fclose(fid);
And when I run the file 'MyFile.txt', it shows in notepad like;
abcab bc
But I want it to show me like:
a
b
c
ab
bc
How can I do that? And one more question. I want to open 'MyFile.txt' automatically when I run the script. What command should I add?
Thanks...
  1 件のコメント
Kan-Hua
Kan-Hua 2014 年 3 月 3 日
Can you elaborate your second question a bit more? I thought you've put the command that opens 'MyFile.txt' in your script, isn't it?

回答 (2 件)

Walter Roberson
Walter Roberson 2014 年 3 月 3 日
fprintf(fid, '%c%c\n', sample.' );
  1 件のコメント
Huseyin
Huseyin 2014 年 3 月 3 日
This time it shows me like;
a b c ab bc
I don't want it to show me in one row. Answers should have their each row.I mean it should look like 5x1 sized matrix in notepad

Kan-Hua
Kan-Hua 2014 年 3 月 3 日
I would suggest you use cell array to store your strings with different lengths unless you have a particular reason to store them using string arrays.
By using the cell array, the code will look like
sample{1}='a ';
sample{2}='b ';
sample{3}='c ';
sample{4}='ab';
sample{5}='bc';
fid=fopen('MyFile.txt','w');
for i=1:length(sample)
fprintf(fid,'%s\n',sample{i});
end
fclose(fid);
  3 件のコメント
Kan-Hua
Kan-Hua 2014 年 3 月 3 日
That's the issue of notepad. If you open 'MyFile.txt' using matlab's built-in editor, it will work fine.
If you insist using notepad, try:
fprintf(fid,'%s\r\n',sample{i});
Huseyin
Huseyin 2014 年 3 月 4 日
編集済み: Huseyin 2014 年 3 月 4 日
Yes it works excellent now! Thanks!

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by