How can I use assignin and eval together in function
5 views (last 30 days)
Show older comments
I have a script that I am trying to turn into a function. The reason being is that I have multiple projects (with individual file folders) that I use the same script for processing. Rather than copying the script from folder to folder and ensuring I am using the latest one version, I'd prefer to simply create and call majority of it as a function.
My issue is my script loads a mat file (with hundreds of variables) and a text file (i.e., tab) that contains the variables names. The code reads the text file row by row and processes the variables listed in each row.
Below are some example rows of tab:
0280;3000;433203.10;100163.60;116.51;SM001_200209;SM001_200304;SM001_201705;SM001_201810;SM001_201497;
0068;3600;433459.90;099725.55;123.29;SM002_200209;SM002_200304;SM002_201705;SM002_201810;SM002_201497;
My issues is I currently use assignin and eval together in my function (I later use the name variable in my code), but they will not work in my script.
for count = 1:size(tab,1)
temp = tab(count,38:end);
try
assignin('base','name',eval(['[' strrep(temp,' ',';') ']']));
name;
catch %#ok<CTCH>
disp('! Wrong variable(s) name or format !')
end
end
I have tried changing the 'base' to 'caller' but it still fails.
I know using assignin and eval is frowned upon, but this script serves my purpose.
Is there any way to get the above to work in a function?
3 Comments
Stephen23
on 10 Apr 2019
Mike Greene's "Answer" moved here:
My preloaded *.mat file includes the variables: SM001_200209, SM001_200304, etc. Each variable is simply 2 columns of double data.
What my code does is it looks at each row of the tab file (e.g., ;SM001_200209;SM001_200304;SM001_201705;SM001_201810;SM001_201497) and combines those 5 variables into a single variable called name.
Guillaume
on 10 Apr 2019
"Rather than copying the script from folder to folder and ensuring I am using the latest one version"
Scary! Matlab has built-in support for subversion and git. You should be using version control
"I'd prefer to simply create and call majority of it as a function."
A laudable goal and the way to go indeed. Don't use scripts.
"I know using assignin and eval is frowned upon, but this script serves my purpose."
Gah! No, it clearly doesn't since it forces you to move the code around or can't be easily morphed into a function.
I'm sorry but the answers you're going to get are don't use eval and assignin. You will find that not using them will actually make your code easier to use and more flexible.
Accepted Answer
Stephen23
on 10 Apr 2019
Edited: Stephen23
on 10 Apr 2019
Your code concept forces you to write slow, complex, obfuscated, buggy, and hard to debug code:
You can easily avoid your bad code design by simply loading into an output variable (which is a structure):
and using dynamic fieldnames:
An outline of what you should do:
V = {varnames in a cell array}; % one row of names file
S = load(...,V{:});
N = numel(V);
C = cell(1,N);
for k = 1:N
C{k} = S.(V{k});
end
... "combine" your data here, e.g.:
data = horzcat(C{:})
and then, if you put this inside a function, simply return that data as an output argument (which is much more efficient and less buggy that assignin).
More Answers (0)
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!