Output of changing number of columns

4 ビュー (過去 30 日間)
Vlad Kaminski
Vlad Kaminski 2015 年 4 月 20 日
コメント済み: Vlad Kaminski 2015 年 4 月 20 日
Dear all, I'm running Matlab 2011a,
Here's the situation: I'm using the fprintf function to output the mat.structure into a file. I'm normally using something like
fprintf(out, '%14f %14f %14f %14f', array); this is true for 4 column format,
However in my "mat.structure(i)", the number of columns is changing with (i).
QUESTION: How can I specify a CHANGING format using fprintf? Is there a better function to suite my needs?
Thanks in advance!

回答 (2 件)

Niels
Niels 2015 年 4 月 20 日
I believe a simple repmat should do the trick:
fprintf(out, repmat('%14f ',[1,size(array,2)]), array);
  1 件のコメント
Vlad Kaminski
Vlad Kaminski 2015 年 4 月 20 日
Thank you! This worked

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


Stephen23
Stephen23 2015 年 4 月 20 日
編集済み: Stephen23 2015 年 4 月 20 日
Here are two ways that you can allow for a variable number of columns and also not introduce an extra space character before/after the text:
1. use repmat on the format string, and then shorten it:
>> A = 1:3;
>> fmt = repmat(' %2f',1,size(A,2));
>> fprintf(fmt(2:end),A)
1.000000 2.000000 3.000000>>
2. use two fprintf calls (if more than one column):
>> fprintf('%2f',A(1)), fprintf(' %2f',A(2:end))
1.000000 2.000000 3.000000>>
A naive repmat on the format string will produce a leading/trailing space character.

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by