store variables with ascending names in array

1 view (last 30 days)
Hello,
Can somebody help me with that?
I am using the PDE toolbox in matlab and I have to create ns and sf variables using the following code.
ns = char('R1','R2','R3','R4','R5','R6','R7');
ns = ns';
sf = 'R1+R2+R3+R4+R5+R6+R7';
It is easy when you have to do it 7 times but I need to do it 12500!!! I believe I need to create variables with ascending names and store them in ns and then add them in sf but I cannot do it!
Many thanks
  1 Comment
Evita
Evita on 16 Jun 2017
Hello,
Many thanks for that but it does not help a lot because although it creates the variables, I need to store them in an array with dimensions 1*12500.
Array=[R1 R2 R3 .... R12500]
In your example the answer is a single cell with R1R2R3R4...R12500 as a single value.

Sign in to comment.

Accepted Answer

MathReallyWorks
MathReallyWorks on 16 Jun 2017
Hello,
Use this for creating as many variables as you want:
for i=1:10 %I have taken 10 just for example and explanation. You can take 12500.
s1 = 'R';
s2 = num2str(i);
s = strcat(s1,s2)
end
Copy and paste above code on command window to understand properly.
  1 Comment
Evita
Evita on 16 Jun 2017
FYI I found it!!! Many thanks again
ns=cell(1,numSub);
for i=1:numSub;
s1=['R'];
s2=[num2str(i)];
temp=strcat(s1,s2);
ns{1,i}=temp;
end
ns=char(ns);
ns=ns';

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!