HELP REQUIRED IN PROGRAMMING

조회 수: 1 (최근 30일)
adarsh
adarsh 2018년 2월 18일
편집: John D'Errico 2018년 2월 22일
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
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
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
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
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
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
Guillaume
Guillaume 2018년 2월 22일
편집: Guillaume 2018년 2월 22일
Chalk another one for the "this is not a good answer" club. There may be some rare cases where eval may be useful. The present case is absolutely, definitively, not one of them. Creating 90 d and 50 P variables is complete madness.
John D'Errico
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 CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by