|
"James " <cress_james@cat.com> wrote in message
news:jfvjta$saq$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <jfuchr$hcm$1@newscl01ah.mathworks.com>...
*snip*
> Yeah I kinda figured that out the hard way. But in the midst of committing
> Harikari I actually figured out a pretty cool trick to do what I wanted.
> So to go a little deeper into my problem I actually had a number of data
> sets that may or may not have the same variables in it that I needed to be
> put together in a single data set. With some work I was able to filter out
> the fieldnames that weren't common and load them into a struct. So now I
> have a struct with that still doesn't have the data as a continuous data
> set so I converted it to a cell array and merged the data into a
> continuous data set. I then converted the cell array to a struct with cell
> to struct. Here is the trick I found .... If I save the struct as a mat
> file and then reload it I end up with all my variables loaded with the
> data.
Yes, and that "trick" is a BAD thing.
Suppose your MAT-file myMatFile.mat contains a variable named alpha that is
the scalar value 5. You would expect this code to display the value 5,
right?
function myfunction
load('myMatFile.mat');
disp(alpha)
It doesn't. Instead you get an error about calling ALPHA with too few input
arguments. You "poofed" the variable alpha into the workspace at runtime.
However, MATLAB has already parsed the function file to try to determine to
what each identifier referred and when MATLAB parsed the function file it
"decided" that alpha was a reference to the ALPHA function.
http://www.mathworks.com/help/techdoc/ref/alpha.html
Instead of "poofing" variables, do this:
function myfunction
mydata = load('myMatFile.mat');
if isfield(mydata, 'alpha')
disp(mydata.alpha);
end
or:
function myfunction
mydata = load('myMatFile.mat');
try
disp(mydata.alpha);
catch
% alpha wasn't a variable in myMatFile.mat
% handle this appropriately
end
FYI, avoiding "poofing" is item 7 on Loren's list of best practices.
http://blogs.mathworks.com/loren/2012/01/13/best-practices-for-programming-matlab/
--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
|