Add s.f. to variable

5 ビュー (過去 30 日間)
Marisabel Gonzalez
Marisabel Gonzalez 2019 年 4 月 7 日
編集済み: A. Sawas 2019 年 4 月 8 日
Hi, I was wondering if there is a way to set specifically the number of s.f. to a variable as you do when printing the answer. For instances,
a=2.335444444444
sprintf ('%.2f',a)
ans =
2.34
I want that but not to print the answer. Instead I want to alter the s.f. used in the variable, i.e. my code would be forced to use 2.34 in its calculations.

回答 (1 件)

A. Sawas
A. Sawas 2019 年 4 月 8 日
編集済み: A. Sawas 2019 年 4 月 8 日
I guess you mean by s.f. the format string in the sprintf function which is '%.2f' in your example.
There is a way to specify variable number of characters by using asterisks (*), like this example:
% 6 is the width
% 3 is the number of decimals
% 0.123456 is the number to be formatted
sprintf('%*.*f', 6, 3, 0.123456)
Another longer way would be to use sprintf to build your format string.
fs = sprintf('%%%d.%df',6,3);
sprintf(fs, 0.123456);

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by