How to create new variables in batches with strcat
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
2 votes
Share a link to this question
Hi,
What I want to do is something like
for m=1:11
for i=1:3
for k=1:5
for x=1:9
strcat(InputCell{m,1},'_', InputCell{1,i},'_',InputCell{m,i}{k},'_',InputCell{m,i}{k,2}{x}) = xlsread(filename,'strcat(InputCell{m,1},'_', InputCell{1,i},'_',InputCell{m,i}{k})', 'InputCell{m,i}{k,2}{x}')
end
end
end
end
Where strcat () creates a bunch of new variable names and xlsread reads the corresponding sheet and range... but strcat can only create string text, how can I let Matlab take that as new variable?
And I suppose the xlsread part also doesn't work as it is now... just let you guys know what I am thinking of ...
Thanks a lot!
Accepted Answer
9 votes
8 Comments
TingTing
on 10 Apr 2015
Thank you very much for your answer. However, I have one extra question:
Is eval function that bad? Sometimes I do need it. For example, in the following case, can you see if eval is that bad and if there is another solution?
I have stored strings in a cell array and by taking some of them and concatenating, I get new strings that correspond to variable names and I need to access the values of these variables, so I wrote
eval(strcat(InputCell{m,1},'_', InputCell{1,i},'_',InputCell{m,i}{k},'_',InputCell{m,i}{k,2}{x}))...
It gives me what I want, but having read those posts about how bad eval is, I wonder if I need another solution...
Thanks.
Yes, you need another solution.
"Sometimes I do need it": actually, probably not. I have seen millions of lines of MATLAB code, and never have I seen good code than really needed eval.
What you are doing is mixing data (those string values) with the abstraction that is being used to represent it (the variables), but these are really completely different paradigms. The names of variables are not a robust place to store data, which really deserves its own storage, in a cell or structure array or the like.
eval is not bad, it is just very very powerful, and like any powerful tool it needs to be used extremely carefully. MATLAB themselves have already written a whole webpage with the title "Alternatives to the eval Function", which was the first link that I gave in my answer: if MATLAB themselves already advise that you should prefer other solutions to using eval, why are you asking me?
The MATLAB IDE includes many helpful tools that give code hints, list input arguments, and help to complete functions, check your code is efficiently written, show errors and warnings, and lots of other useful things too. Simply click on a variable in an Mfile and the editor will locate every instance of that variable. Once you start to use the f1 button and the other code helpers you will realize that MATLAB is helping you to write code: code checking, variable highlighting, etc. But when you use eval, none of those tools work.
eval also makes it very easy to completely obscure the code intent: Can you tell me what this code does?:
x1 = [119 101 98 40 39 104 116 116 112 58 47 47 119 119 119];
x2 = [ 46 121 111 117 116 117 98 101 46 99 111 109 47 119 97];
x3 = [116 99 104 63 118 61 100 81 119 52 119 57 87 103 88];
x4 = [ 99 81 39 44 39 45 98 114 111 119 115 101 114 39 41];
eval(char([x1 x2 x3 x4]))
Did you run it? Do you realize that although it did something relatively harmless, it could have deleted everything on your harddrive, or send an insulting email to all of your business contacts, or simply altered any variables in the MATLAB workspace. This is not a secure way of programming, it is actually a lazy and dangerous way to program that beginners think is wonderful.
For some reason beginners think it is great that variables pop in and out of existence in different workspaces, but really it is just slow, impossible to debug properly, and makes keeping track of variables a complete pain. Professional code definitely does not have variables magically appearing and disappearing everywhere, so why would you?
Learn to use cell arrays, structures and ... multidimensional numeric arrays!
Robert Cumming
on 20 Apr 2015
+1 for the eval example!! :)
Jan
on 19 Jul 2015
@Yiting: Is eval function that bad? Yes, it is.
xian zhao
on 15 Jan 2018
I need a initialize block, and I declare a variable as the flag of initialiazation. However if other mdl include this block, the flag variable will be overrighted.
Walter Roberson
on 15 Jan 2018
xian zhao you appear to be referring to Simulink in a way that does not have to do with the Question here in this posting. Please create a new Question.
xian zhao
on 15 Jan 2018
At the begainning, I declare a variable whose name will change as above. However, it's suggest as explain above.
Walter Roberson
on 15 Jan 2018
Simulink itself is not able to use variable names that change like that. You would have to be using something like a tunable parameter and set_param() calls if you were trying to use something like that, and there is simply no reason to want to do so when you could instead use a fixed variable and change its value instead of the name.
If you are trying to do this in a MATLAB Function block that is referenced by Simulink, there there is no hope that it could work except when Acceleration was completely off. For any other Acceleration, Simulink needs to compile to blocks (at least partly) and that compilation cannot use dynamic variable names.
More Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)