MATLAB Answers

0

Decent way of loading and saving variables

Asked by Willem-Jan de Goeij on 19 Feb 2012
I would like to load and save my variables in Matlab like this:
[height, weight] = load('measurements.mat');
%%calculatate BMI
save('BMI.mat', BMI);
I believe this is not possible in Matlab.
Why do I want it like this?
1) I want to be able to store my filenames in strings, so for example
path = ... %can have different values, for example server or local
measurementFile = [path '\measurements.mat'];
[height, weight] = load(measurementFile);
Unfortunately,
load measurementFile height weight
does not work.
2) I want to see if I load any variable which I don't use.
So suppose I had
load(measurementFile, 'height', 'weight', 'age');
But I don't use age. Then I want to get an warning in the editor.
If I had
[height, weight, age] = load(measurementFile);
I would get a warning, which is exactly what I want!
3) Using loaded variables in a parfor loop also gives an error.
For example:
load(measurementFile, 'height', 'weight');
parfor ii=1:nPeople
BMI(ii) = weight(ii) / height(ii)^2;
end
gives an error:
Undefined function or method 'weight' for input arguments of type 'double'.
Solving it by using:
height = height;
weight = weight;
looks ugly to me.
How do you think about this?
Could this be new functionality in R2012b?

  0 Comments

Sign in to comment.

Tags

3 Answers

Answer by the cyclist
on 19 Feb 2012

You can absolutely do the first thing you want. You can load from a MAT file into a structure. You can also use the save syntax you want. I think the problem with the way you tried to do it is that you did not put BMI into single quotes, as I do below:
If the file bmiData has vectors height and weight (of the same shape), then this example should work:
bmiStruct = load('bmiData');
h = bmiStruct.height;
w = bmiStruct.weight;
bmi = w./h.^2;
save('bmiData.mat','bmi','-append')
In place of the explicit strings 'bmiData' and 'bmi' in the save command, you could have used a string variable that stores the name, like this:
dataFile = 'bmiData.mat';
save(dataFile,'bmi','-append')
In your second problem, the syntax
load('bmiData.mat','weight','height','age')
gives a warning that age is not in the file, at least in 2011b.
I'm very confused by your third problem. I can run the exact example you give, with no problem, when height and weight are vectors stored in my file.

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 19 Feb 2012

For point #3, I suspect it is not parfor that is the issue, but rather that it is using routines that end with a specific 'end' statement, forming a closure. When you form a closure (the newer style recommended programming), then statements such as
load(measurementFile, 'height', 'weight');
attempt to "poof" variables in to existence after the routine has already been parsed. Any assignment that is not explicit in the code is a problem. Initialize the variables before the load() to get around the problem
height = []; weight = [];
load(measurementFile, 'height', 'weight');
Or, better yet, use the structure assignment form of load()

  0 Comments

Sign in to comment.


Answer by Willem-Jan de Goeij on 19 Feb 2012

1) Using the structure assignment is a solution. It's not very short code though, you need an extra line for every variable you want to load.
Furthermore, if you only want to load a subset of all variables, you need to write the variable names three times (!!!) in your code:
bmiStruct = load('bmiData.mat','height','weight'); % do not load age
height = bmiStruct.height;
weight = bmiStruct.weight;
2) Consider your solution, cyclist:
function cyclist
dataFile = 'bmiData.mat';
bmiStruct = load(dataFile);
h = bmiStruct.height;
w = bmiStruct.weight;
bmi = w./h.^2;
save(dataFile,'bmi','-append');
end
Then i get the following warning on line 6 in the Matlab editor (R2010b): The value assigned to bmi might be unused.

  1 Comment

Although I chose to define a new variable outside the structure, to do the bmi calculation, that is not necessary. You can do later calculations directly on the variables inside the structure. So, depending on code efficiency and readability, you can choose your preferred method.
It is true that the MATLAB warnings sometimes cannot detect all uses of a variable. Given that you have understood the warning, you can always now choose to suppress it (which you can do by right-clicking where the warning appears, and choosing from the menu).

Sign in to comment.