HELP REQUIRED IN PROGRAMMING
Show older comments
if i want to give; d1,d2,d3......d90 all having value as 2 i.e; d1,d2,d3....d90=2 P1,P2,P3......P50 all having value as 18 i.e. P1,P2,P3....P50=18 then how to give these statement in MATLAB
1 Comment
Creating variables with indices hidden in the name is a bad programming patter. See:
- FAQ: Create variables A1, A2, ...
- Tutorial: Do not EVAL
- MATLAB documentation: Why Avoid the eval Function?
A method to create "numbered variables" is less useful than using a clean programming style by using arrays and indices.
@adarsh: Your other questions contains code, which suffers from the same problem: Question 378029, Question 383271, Question 378029, Question 376013. There we find the same programming pattern of numbered variables. It is time to learn a better method to write clean and efficient code. Use arrays instead of a complicated bunch of numbered variables.
Accepted Answer
More Answers (1)
John BG
on 21 Feb 2018
Hi Adarsh
there's a very easy way to obtain the variables you need with command evalin:
1. Define the following parameters
What variable strings to use:
str1={'d';'P';'sbt'}
What range for each variable
range=[9;5;12]
What value for each set of variables
val=[2;18;21]
the above values are just example, change accordingly
2.
Because the range of one or more variables can easily exceed 10, the simple approach
str_rng=num2str([1:1:range(k)]'); % do not use
cannot be used because it adds a blank ahead of the numeral for the values [1:9]
3.
Using a cell to build strings for evalin
for k=1:1:numel(str1)
str_rng={};
for s=1:1:range(k)
str_rng=[str_rng;num2str(s)];
end
for s=1:1:range(k)
evalin('base',[str1{k} str_rng{s} '=' num2str(val(k))])
end
end
Now in your workspace you have all the variables generated as required.
Comment:
Some experts discourage the use of evalin in favor of building matrices containing all relevant data. That is particularly useful when reading some one else's code.
Yet I agree with you that sometimes there's need to generate independent variables as you have described in your question.
Adarsh
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
5 Comments
Not only "some" experts, but all experts and the documentation of Matlab recommend not to create variables dynamically. See the exhaustive explanation Tutorial: Do not EVAL and MATLAB docs: Why Avoid the eval Function?
range(k) is meaningless here, because k is a scalar inside the loop. The editor shows a MLint-warning because of the iterative growing of str_rng - serious code would use a pre-allocation. evalin('base') creates the variables in the base workspace, where they are not needed. Therefore this is a bad implementation of a bad idea.
Compare this suggestion from John D'Errico:
d = repmat(2,1,90);
with John BG's proposal:
for k=1:1:numel(str1)
str_rng={};
for s=1:1:range(k)
str_rng=[str_rng;num2str(s)];
end
for s=1:1:range(k)
evalin('base',[str1{k} str_rng{s} '=' num2str(val(k))])
end
end
Now some simple questions:
- Which of these will be slower?
- Which of these will be harder to debug?
- Which of these will waste more of the programmer's time?
- Which of these makes further processing of that data more difficult?
- Which of these requires multiple functions, temporary variables, and for loops, just to store exactly the same data?
- Which of these totally destroys any possible JIT code acceleration, thus forcing beginners into writing much slower code?
@adarsh and other readers: This is a bad choice of an accepted answer. John BG's code does not create the wanted variables where they are needed and even if you modify it to work properly, the eval methods are known to create more problems than they seem to solve. This topic is discussed several times each week and everybody but John BG suggests better, cleaner and more efficient solutions.
Several experienced programmers offered you help which is useful in a long term view. And John BG's eval suggestion means drilling a hole in your knee. Of course, it is your choice.
John D'Errico
on 22 Feb 2018
Edited: John D'Errico
on 22 Feb 2018
It is madness, because the next step is what will you do with all those crap variables? The answer is simple. They will need to create OTHER variables, with similar names. So they will now be creating E1-E90, and Q1-Q50 etc. Or they will want to create sums of those variables. So they will be asking how to perform a sum over all those variables. Etc. Quick steps into pure programming hell.
We can say thanks to John BG for helping to bring yet another person into a programming style that will only earn the ire of anyone who ever tries to use or even read their code from hell. And of course, when adarsh tries to debug their own code, expect many more frenzied questions, about how do I debug this crappy code. Given that the alternative is so trivially simple to write and use ... way to go John BG.
Categories
Find more on Matrix Indexing 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!