I have an fprintf command
fprintf('\n %s \n%0.3f',Data{:})
where my data contains a sentence and then an array. I want the decimal places for the array to only go to 3 which seems only work for every other number. So every thing comes out fine but it looks like this:
Sentence
1.784
2.862691e+000
2.737
2.855501e+000
2.976
i dont want the exponentials

1 件のコメント

Jan
Jan 2011 年 10 月 1 日
Please post the contents of the Data cell. I cannot imagine, that the posted format string can create the shown answer.

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

 採用された回答

Image Analyst
Image Analyst 2011 年 10 月 1 日

0 投票

Your format specifier string expects Data to have the form "string number string number string number etc." Apparently Data{1} is a string but I'm not sure Data{3} and Data{5} are. They're probably numbers, not strings, so when it tries to print those numbers out as strings (according to %s) you get the unexpected results. Show us how you constructed Data.

5 件のコメント

Zach
Zach 2011 年 10 月 1 日
Data={'The calculated means for ERP0 are ','The standard deviations for ERP0 are ', 'The calculated means for ERP08 ', 'The standard deviations for ERP08 are ';mean0 stdev0 mean08 stdev08};
fprintf('\n %s \n%0.3f',Data{:})
Zach
Zach 2011 年 10 月 1 日
where mean0 stdev0 stdev08 and meaon08 are arrays
Jan
Jan 2011 年 10 月 1 日
@Zach: Please post something we can run. It is fine, that mean0 is "an array", but we cannot know its dimension. Without knwoing the exact problem, all trials to assist you must be based on wild guessing.
Zach
Zach 2011 年 10 月 1 日
Data={'The calculated means for ERP0 are ','The standard deviations for ERP0 are ', 'The calculated means for ERP08 ', 'The standard deviations for ERP08 are ';[1,2,3] [4,5,6] [7,8,9] [10,11,12]};
fprintf('\n %s \n%0.3f',Data{:})
Image Analyst
Image Analyst 2011 年 10 月 1 日
You have 3 numbers for each string, so you need to do
fprintf('\n %s \n%0.3f %.3f %.3f',Data{:})

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

その他の回答 (1 件)

Jan
Jan 2011 年 10 月 1 日

1 投票

I guess that your Data are:
Data = {'Sentence', [1.784, 2.862691, 2.737, 2.855501, 2.976]};
When you use the format string '\n %s \n%0.3f' every second number is displayed using the '%s' format. So you have to split the string from the numerical data:
fprintf('\n %s', Data{1});
fprintf(' \n%0.3f', Data{2});
Or perhaps: fprintf(' \n%0.3f', Data{2:end});

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by