1 Ansicht (letzte 30 Tage)
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 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
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:
What range for each variable
What value for each set of variables
the above values are just example, change accordingly
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]
Using a cell to build strings for evalin
for k=1:1:numel(str1)
for s=1:1:range(k)
for s=1:1:range(k)
evalin('base',[str1{k} str_rng{s} '=' num2str(val(k))])
Now in your workspace you have all the variables generated as required.
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.
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
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.


Find more on Scope Variables and Generate Names 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!

Translated by