HELP REQUIRED IN PROGRAMMING

2 Ansichten (letzte 30 Tage)
adarsh
adarsh am 18 Feb. 2018
Bearbeitet: John D'Errico am 22 Feb. 2018
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 Kommentar
Jan
Jan am 22 Feb. 2018
Bearbeitet: Jan am 22 Feb. 2018
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 18 Feb. 2018
Bearbeitet: John D'Errico am 18 Feb. 2018
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 Kommentare
Steven Lord
Steven Lord am 22 Feb. 2018
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 am 22 Feb. 2018
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John BG
John BG am 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 Kommentare
Guillaume
Guillaume am 22 Feb. 2018
Bearbeitet: Guillaume am 22 Feb. 2018
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 am 22 Feb. 2018
Bearbeitet: John D'Errico am 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by