how to save a file in ".m" format using command

9 views (last 30 days)
I need to save some values in .m format. when I save the values in .mat format and load it and do calculation, the results are not correct.
s1=load ('ppvalue.mat');
mu1=mean(s1);
c1=s1(1,1); % value=8
b1=mu1(1,1); % value=36.6678
d1=c1-b1; %it gives me 0 (instead of negative value)

Answers (1)

Brian B
Brian B on 8 Feb 2013
Edited: Brian B on 8 Feb 2013
You should not save data in an m-file; a mat-file is the best way. I believe the problem is how you are loading the data. If you call
load('ppvalue.mat')
the variables saved in the file are created in the current workspace with their values as they were saved in the file. If, instead, you assign the output of the call to load to a variable as in
s1 = load('ppvalue.mat');
then s1 will be a scalar structure with one field for each variable saved in the mat-file. The field names will be the names of the original variables.
Your code above attempts to compute the mean of the struct s1, which is not defined. Instead, try
mu1 = mean(s1.varname)
where varname is the original name of the variable that was saved to the mat-file. To see what it is, just look at s1 in the command window.

Community Treasure Hunt

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

Start Hunting!