Use of num2str to name xml-files in a loop

4 ビュー (過去 30 日間)
Martin
Martin 2012 年 5 月 15 日
Hello,
I'm using a code to write xml-files for some data. The code is:
fid = fopen('file.xml', 'w');
if fid == -1; error('Cannot open file for writing'); end
fprintf(fid, 'Hello ');
fprintf(fid, '%g, ', x(1:end-1));
fprintf(fid, '%g', x(end));
fprintf(fid, ' This is the data.');
fclose(fid);
Since I need to write more than one xml-file, I would like to do it with a loop. My problem is that I need to name each file, e.g. file1.xml, file2.xml and so on. I have tried to do this with a num2str command in a loop, but I can't get it to work. Is there anyone out there who can help me with this problem?
Hope to hear from someone.
//Martin.

採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 15 日

その他の回答 (1 件)

Martin
Martin 2012 年 5 月 15 日
I have looked at that link and tried to implement it (I don't know much about data ex- and import). I then write;
for k=1:3
textFilename = ['file' num2str(k) '.xml']
fid = fopen(textFilename, 'rt');
fprintf(fid, 'Hello ');
fprintf(fid, '%g, ', x(1:end-1));
fprintf(fid, '%g', x(end));
fprintf(fid, ' This is the data.');
fclose(fid);
end
This doesn't work - there's an error using fprintf. It's crucial that I can write the text in the files. Anyone who has a way to do it?
//Martin.
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 5 月 15 日
You would probably have more success if you opened the file for writing rather than reading.
Martin
Martin 2012 年 5 月 15 日
Hehe, I guess you're right. As I said, I'm new to ex- and import - and apparently also an idiot ;)
Thank you for the help.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by