Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to save variales with different accuracy

Subject: How to save variales with different accuracy

From: evan um

Date: 12 Dec, 2008 05:48:02

Message: 1 of 6

Hi all,

I would like to get some helps.
I try to save several different variable as shown below:
save file_name var1 var2 var3 var4 var5 -double -ascii

However, I want to save the first three variables in single precision format, whereas the last two variables should be in double precison format. Can we do this in MATLAB?
In advance, I appreciate your helps!

Best,
Evan

Subject: How to save variales with different accuracy

From: us

Date: 12 Dec, 2008 09:52:02

Message: 2 of 6

"evan um"
> I try to save several different variable as shown below:
> save file_name var1 var2 var3 var4 var5 -double -ascii
> However, I want to save the first three variables in single precision format, whereas the last two variables should be in double precison format. Can we do this in MATLAB...

yes, but SAVE does not do what you want; you will have to write your own function using low-level file i/o functions (which is easy enough)
see

     help fopen;
     help fprintf;
     help fclose;

us

Subject: How to save variales with different accuracy

From: Walter Roberson

Date: 12 Dec, 2008 15:29:48

Message: 3 of 6

evan um wrote:

> I would like to get some helps.
> I try to save several different variable as shown below:
> save file_name var1 var2 var3 var4 var5 -double -ascii
 
> However, I want to save the first three variables in single precision format, whereas the
> last two variables should be in double precison format. Can we do this in MATLAB?

Break it in to two commands, using one double precision and the other not. Use
-append for the second one.

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: How to save variales with different accuracy

From: us

Date: 12 Dec, 2008 15:49:02

Message: 4 of 6

Walter Roberson
> > I would like to get some helps.
> > I try to save several different variable as shown below:
> > save file_name var1 var2 var3 var4 var5 -double -ascii
> > However, I want to save the first three variables in single precision format, whereas the
> > last two variables should be in double precison format. Can we do this in MATLAB?
>
> Break it in to two commands, using one double precision and the other not. Use
> -append for the second one.

as was said earlier:
SAVE cannot do this...

     s=single(pi);
     save goo s -ascii
%{
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 's' not written to file.
%}

us

Subject: How to save variales with different accuracy

From: Walter Roberson

Date: 12 Dec, 2008 18:42:08

Message: 5 of 6

us wrote:
> Walter Roberson

>>> I try to save several different variable as shown below:
>>> save file_name var1 var2 var3 var4 var5 -double -ascii
>>> However, I want to save the first three variables in single precision format, whereas the
>>> last two variables should be in double precison format. Can we do this in MATLAB?

>> Break it in to two commands, using one double precision and the other not. Use
>> -append for the second one.

> as was said earlier:
> SAVE cannot do this...

> s=single(pi);
> save goo s -ascii
> %{
> Warning: Attempt to write an unsupported data type to an ASCII file.
> Variable 's' not written to file.
> %}

The original poster asked to save the variables in single precision format, but it
is not clear from the original posting whether the values are class single or class
double. My interpretation was that the values were all class double but the -output-
format requested was single for some and double for others. If that is the case, then

save file_name var1 var2 var3 -ascii
save file_name var4 var5 -ascii -double -append


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: How to save variales with different accuracy

From: us

Date: 12 Dec, 2008 19:27:02

Message: 6 of 6

Walter Roberson
> The original poster asked to save the variables in single precision format, but it
> is not clear from the original posting whether the values are class single or class
> double. My interpretation was that the values were all class double but the -output-
> format requested was single for some and double for others. If that is the case, then
> save file_name var1 var2 var3 -ascii
> save file_name var4 var5 -ascii -double -append

NO...
please, carefully check your solution before posting

     v=pi; % a double
     save goo.foo v -double -ascii;
     save goo.foo v -ascii -append;
     type goo.foo
%{
     3.1415926535897931e+000 % the apparent DOUBLE
     3.1415927e+000 % the apparent SINGLE
%}

% now, the poster might think that he/she gets the SINGLE(val) back by
     val=load('goo.foo','-ascii');
% which is wrong
     format long;
     val
%{
% this seems to look ok...
val =
     3.141592653589793 % the assumed DOUBLE
     3.141592700000000 % the assumed SINGLE
%}

% BUT
     sprintf('%30.20f\n',v,single(v),val,single(val(2)))
%{
     3.14159265358979310000 % double pi
     3.14159274101257320000 % true SINGLE(pi) + FP noise
     3.14159265358979310000 % val(1) from apparent DOUBLE
     3.14159269999999990000 % val(2) from apparent SINGLE
     3.14159274101257320000 % apparent SINGLE to true SINGLE + FP noise
%}

clearly shows that things are not that easy - and - whoever receives the ascii file would still have to typecast the values appropriately...

just a note...
us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us