### Tags: 4significant, appears, digits, everyonei, figures, format, matlab, output, programming, significant, specify

# problem with significant figures

On Programmer » Matlab

3,922 words with 7 Comments; publish: Tue, 29 Apr 2008 00:09:00 GMT; (200125.00, « »)

Hi everyone

I'd like to know how i can specify an output format with 4

significant digits in Matlab.

E.G.:

1.2345 appears as 1.235

6.7892e-01 appears as 0.6789

thanks !

*http://matlab.questionfor.info/q_matlab_48081.html*

All Comments

Leave a comment...

- 7 Comments
- Check:
help num2str

as well as

help sprintf

Regards

Carlos

#1; Tue, 29 Apr 2008 00:10:00 GMT

- Check:
- I tried several combinations with num2str, et sprintf
(with '%.4f' for example, but nothing was perfect for every

case) I really don't know what to do :(

#2; Tue, 29 Apr 2008 00:11:00 GMT

- I tried several combinations with num2str, et sprintf
- Francois Guillot wrote:
> I tried several combinations with num2str, et sprintf

> (with '%.4f' for example, but nothing was perfect for every

> case) I really don't know what to do :(

For all possible ranges it's not trivial although you might try %g

although I'm not positive it will solve all problems, either.

The only actual way is probably to customize a function that scales the

value and determines how to format it based on the magnitude.

#3; Tue, 29 Apr 2008 00:12:00 GMT

- Francois Guillot wrote:
- "Francois Guillot":
<SNIP four-non-zeros only...

one of the solutions

% the data

d=[

1.23456789

0.000123456789

12.3456789

0.00000123456789

123

0.001

];

nd=4;

% the engine

v=sprintf('%1000.500f\n',d);

v=strread(v,'%s');

ix=regexp(v,'[123456789]');

v=cellfun(.matlab.questionfor.info.(x,y) x(1:y(min([nd,numel(y)]))),...

v,ix,'uni',false);

r=sprintf('%s\n',v{:});

% the result

disp(r);

%{

1.234

0.0001234

12.34

0.000001234

123

0.001

%}

us

#4; Tue, 29 Apr 2008 00:13:00 GMT

- "Francois Guillot":
- "us " <us.matlab.questionfor.info.neurol.unizh.ch> wrote in message
> % the result

> disp(r);

> %{

> 1.234

> 0.0001234

> 12.34

> 0.000001234

> 123

> 0.001

> %}

It's not exactly what I want.

1.234 ok

0.0001234 should be 0.000

12.34 ok

0.000001234 should be 0.000

123 should be 123.0

0.001 is ok

I loose some informations on the numbers '0.000' but I have

to format it nicely with only 4 digits, no matter what

happens, to put it in a nice array.

#5; Tue, 29 Apr 2008 00:14:00 GMT

- "us " <us.matlab.questionfor.info.neurol.unizh.ch> wrote in message
- "Francois Guillot":
<SNIP now at: four-digits only

well, one of the other solutions

% the data

d=[

1.23456789

0.000123456789

12.3456789

0.00000123456789

123

0.001

];

nd=4;

% the engine

v=sprintf('%f\n',d);

v=strread(v,'%s');

ix=regexp(v,'\d');

v=cellfun(.matlab.questionfor.info.(x,y) x(1:y(min([nd,numel(y)]))),...

v,ix,'uni',false);

r=sprintf('%s\n',v{:});

% the result

disp(r);

%{

1.234

0.000

12.34

0.000

123.0

0.001

%}

us

#6; Tue, 29 Apr 2008 00:15:00 GMT

- "Francois Guillot":
- Francois Guillot wrote:
> "us " <us.matlab.questionfor.info.neurol.unizh.ch> wrote in message

> It's not exactly what I want.

> 1.234 ok

> 0.0001234 should be 0.000

> 12.34 ok

> 0.000001234 should be 0.000

> 123 should be 123.0

> 0.001 is ok

> I loose some informations on the numbers '0.000' but I have

> to format it nicely with only 4 digits, no matter what

> happens, to put it in a nice array.

Didn't see us' post so couldn't see his algorithm but to do what you're

asking would require using the size of the data to decide the field

width and format as previously suggested.

For example, the solution to one particular case above would be

ans = 0.000

But for

ans = 123.000

the precision of .3 overrides the field width 5.

Hence, you would need the precision width to be 3 - fix(log10(x))

(I think, didn't actually test that but log10(123) = 2.xxx, so for that

case works. Of course, there can't be any value larger than 9999.

embedded in a field of 5, either, so don't know what your wishes would

be for outside that range.)

#7; Tue, 29 Apr 2008 00:16:00 GMT

- Francois Guillot wrote: