Create & Save Off Dynamically Named Variables

16 次查看(过去 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
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
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
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 个评论
Richard Hopple
Richard Hopple 2017-9-7
Can't accept because I can't enforce downstream usage of this tool. Needs to be done at time of run
Walter Roberson
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by