Decent way of loading and saving variables
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
the cyclist
2012 年 2 月 19 日
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 件のコメント
Walter Roberson
2012 年 2 月 19 日
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 件のコメント
Willem-Jan de Goeij
2012 年 2 月 19 日
1 件のコメント
the cyclist
2012 年 2 月 20 日
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).
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT-Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!