フィルターのクリア

fprintf problem printing to new line

2 ビュー (過去 30 日間)
James
James 2013 年 3 月 25 日
I have a problem with fprintf whereby it will not create a newline as the delimiter.
I am reading file names in from a structured array using a loop:
fid = fopen('filenameobs.txt', 'wt');
for i=1:length(filenameobs)
fprintf(fid,'%s',filenameobs(i,1).newname,'\r\n');
end
fclose(fid);
The output ends up as:
CONC_2010_09_23_02_19\r\nCONC_2010_09_25_04_54\r\nCONC_2010_09_25_21_31\r\n
rather than each file name on a new line.
I assume the issue is my loop as examples online don't seem to have the problem but I'm unsure as to how to get around it.
Any help would be appreciated! Thanks!
James
  1 件のコメント
James
James 2013 年 4 月 2 日
I'm now trying to print separate vector's out to a text file using similar code but I'm having similar problems. I want all values from each vector to either be on the same line or column and then the next vector to be on the next column of line respectively. My code is:
fid = fopen('statsT.txt', 'wt');
for i=1:fnox
fprintf(fid,'%s\r\n',stats(i,1).biasT,);
end
fclose(fid);
How would I alter my code to do this?

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

採用された回答

Walter Roberson
Walter Roberson 2013 年 3 月 25 日
fprintf(fid,'%s\r\n',filenameobs(i,1).newname);
Caution: as you opened with 'wt', if you are using MS Windows, each occurrence of \n is going to automatically replaced by \r\n, so you would end up with two \r in a row.
If you want to force \r\n always, and no sometimes-present extra \r, then fopen with 'w' instead of 'wt'.
If you want \r\n to appear or not as appropriate for the operating system, open with 'wt' and use \n instead of \r\n
  1 件のコメント
James
James 2013 年 3 月 25 日
Brilliant, thanks!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by