Hi,
I have a structure array that I want to write to a file. I initially used evalc to do this, but found that Rody Oldenhuis's toString function gave a more informative string. It turns out, however, that I am not able to write the resulting string to a file (or to stdout) with fprintf. The following:
s = toString(myStruct);
fprintf(s)
returns gibberish, even though s is a character array as it should be. I thought fprintf worked for all strings. What am I missing?

3 件のコメント

David Sanchez
David Sanchez 2013 年 10 月 14 日
At least in my matlab2012a, there is a tostring built in function. Qhich one are you using, the built.in tostring or that from Matlab central?
Jan
Jan 2013 年 10 月 14 日
@Alec: Please show us an example of what "gibberish" means.
Alec Nagel
Alec Nagel 2013 年 10 月 14 日
@David: I am using the Matlab Central one. The builtin tostring function from the DSP toolbox seems to be made for a more specific task.

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

 採用された回答

Jan
Jan 2013 年 10 月 14 日
編集済み: Jan 2013 年 10 月 14 日

0 投票

fprintf works for strings, but interprets escape characters. Compare:
fprintf('hello, this is a \n backslash');
fprintf('%s', 'hello, this is a \n backslash');
Similar problems occur for %. So better use the '%s' format specifier to display strings.
[EDITED after your comment] I assume "multi-line-string" means a CHAR matrix. A short example would have revealed this immediately. Then:
CStr = cellstr(Str);
fprintf('%s\n', CStr{:});
Please care for adding a helpful example to reduce the time we waste with guessing.

3 件のコメント

Alec Nagel
Alec Nagel 2013 年 10 月 14 日
Tried that. Same result.
After fiddling a little more with this, it seems to me that the problem is that fprintf reads the multiline string in the wrong direction (top to bottom before left to right). fprintf(s') gives something slightly better, but ignores the line breaks and introduces a lot of spaces between the fields.
Alec Nagel
Alec Nagel 2013 年 10 月 14 日
[After your edit.] Sorry about that, Jan, I'll make sure I include an example next time.
And thanks for your help! I am very impressed with your work in here.
Jan
Jan 2013 年 10 月 14 日
Thanks, Alec. There is no need to say sorry, because you are the one who suffers most from a delayed solution. It is the nature of a question, that it is not clear in the beginning, which information is important and which is not.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Environment and Settings についてさらに検索

製品

質問済み:

2013 年 10 月 14 日

コメント済み:

Jan
2013 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by