Create & Save Off Dynamically Named Variables
조회 수: 20 (최근 30일)
이전 댓글 표시
So I'm trying to both make and save off a dynamically named variable. I realize that dynamic named variables are bad and hard to use, however, currently I have 5 different versions of test and it's only going to grow. End goal is to load a variable name that makes sense into the workspace. Something like:
fileName='meaningfulFileName';
variableName='meaningfuleVariable';
save(strcat(fileName,'.mat),'variableName');
clear all
then load the file meaningfulFileName.mat and have meaningfulVariable show up in the workspace.
On a side note, these files are being saved off for archive purposes mostly & I don't want the next person to have to remember to change the variable name as soon as they load it.
Thanks in advance!
댓글 수: 8
José-Luis
2017년 9월 7일
편집: José-Luis
2017년 9월 7일
Late to the party:
From what I could get, you need a variable B to exist in your workspace, probably because you have a script that needs it.
The question is then, can't this be solved by making your script a function that takes the file name as an argument. You can then do the pertinent checks to see that you loaded what you expected and name it whatever you want.
I've read the comments here and can't really understand why you want to avoid loading into a structure.
Stephen23
2017년 9월 7일
편집: Stephen23
2017년 9월 7일
@Richard Hopple: your explanation makes no sense: there is no variable B. Your example has a variable named fileName and one named variableName, both of which contain strings. So when you say that you want to "variable B show up in the workspace", what are you referring to? There is no variable B. What is variable B?
In any case, you would be much better of converting all scripts into functions and passing the data properly as input and output arguments, then you avoid this whole problem entirely.
Even if the scripts cannot be changed (e.g. they are provided by an obnoxious professor who insists that they know how to write code properly and isn't COBOL just wonderful) you can still avoid the whole problem by calling the scripts from inside some function:
function [X,Y] = myfun(A,B,C)
script_using_ABC
end
and then you can call that function with the loaded data and the names of the loaded data do not matter:
S = load(...);
[G,H] = myfun(S.blah, S.data, S.whatever)
Trying to magically rename variables is not a robust or efficient solution. Don't get stuck one some idea that your code is perfect and that scripts are brilliant. They aren't, no matter how much some beginners like them. You would have avoided this entire issue if you had written better code right form the start (i.e. functions, not scripts), and not wasted weeks waiting for a solution to a pointless problem.
답변 (1개)
Walter Roberson
2017년 8월 24일
Provide a small routine that loads files for the user under the variable name.
function result = loademup(filename)
filestruct = load(filename);
fn = fieldnames(filestruct);
result = filestruct.(fn{1});
If this is too hard for the user to user, then
function loademup(filename)
filestruct = load(filename);
fn = fieldnames(filestruct);
result = filestruct.(fn{1});
assignin('caller', 'meaningfuleVariable', result)
댓글 수: 3
Walter Roberson
2017년 9월 7일
편집: Walter Roberson
2017년 9월 7일
fileName='A'; variableName='B';
datastruct.(variableName) = name_of_variable_data_is_in;
save(strcat(fileName,'.mat),'datastruct', '-struct');
clear all
Now when someone does a plain load of the mat file, the data will appear in the variable B
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!