- FAQ: Create variables A1, A2, ...
- Tutorial: Do not EVAL
- MATLAB documentation: Why Avoid the eval Function?
HELP REQUIRED IN PROGRAMMING
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2018-2-22
编辑:Jan
2018-2-22
Creating variables with indices hidden in the name is a bad programming patter. See:
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.
采纳的回答
John D'Errico
2018-2-18
编辑:John D'Errico
2018-2-18
Sorry. But this is just a bad idea. A novice style or programming, only headed for worse things to come.
What will you do with those variables? Probably want to create new numbered variables, all on the fly. So you will find ways to create that variables too. UGH. Bad programming. Bad news. Inefficient, buggy code. I'm sorry to have to tell you this, but you will be sorry in the end.
Instead, learn to use arrays and vectors. Learn to use MATLAB. What you are doing is thinking in terms of a spreadsheet. MATLAB is not a spreadsheet. Spreadsheets make it simple to do what you want, because that is how they are designed to work. But instead, there are far easier ways.
d = repmat(2,1,90);
DONE. One line. If you want to access the 17th element of that vector, simple. d(17). If you want to access multiple elements of d, then use something like d(1:2:end) for example, which will access every other element.
Or create d as
d = 2*ones(1,90);
Create P in one of those same ways.
Your code will improve by leaps and bounds, once you start to use MATLAB as it is designed to be used, instead of trying to use MATLAB as a spreadsheet.
6 个评论
Steven Lord
2018-2-22
The array size limit preference can help protect you against the medium case where your array is big but not impossibly big. The fact that MATLAB won't even try to allocate an array with more elements than the number of atoms in the universe will save you in the impossibly big case.
Walter Roberson
2018-2-22
At least twice a year, a user gets upset that MATLAB will not allocate arrays with more elements than the number of atoms in the universe. When they ask for zeros(1:3000) then, By Golly! they expect to be obeyed!
更多回答(1 个)
John BG
2018-2-21
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 个评论
John D'Errico
2018-2-22
编辑:John D'Errico
2018-2-22
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!