# Help creating a loop

2 views (last 30 days)
Noah Wilson on 24 Jun 2019
Commented: Bob Nbob on 25 Jun 2019
Attached is the code in question. Currently I have the variables 'th' and 'Io' currently at one value. I would like to change them so that the user can input a range of values for them. I am thinking that it will be easiest to create a loop for the ranges but I am unsure how to do that and implement it in the existing code where those variables are used. Any help is greatly appreciated. Thanks in advance.

Bob Nbob on 24 Jun 2019
I did not look in great detail at your code, but it seems like you are always going to define your variables ahead of time because the number of input arguments will always be less than seven for how you have things set up. You will need to have th and Io put into the function inputs if you want them to be user input. (I am assuming you know that, but reminding you.)
There are a couple of different options of how you can handle an array input like that. Loops are always an option, either inside or outside the function.
% Outside
th = [1 2 3];
Io = [1 2 3];
for i = 1:length(th)
for j = 1:length(Io)
[outputs] = KraKro_DVedit(Otherinputs,th(i),Io(j));
end
end
Inside looks pretty much the same, define th and Io outside, pass the entire set in, the inside have the same pair of loops, but inside the loops will be the calculations involving th and Io.
It seems like most of your calculations with th and Io are just addition, subtraction, multiplication, and division. It is possible to submit an array into this type of process, and have Matlab automatically evaluate the elements separately, but you will need to make sure the th and Io are the same size, and that all of your mathematical operators become element operators (* becomes .*, addition/subtraction are the same).
Keep in mind that the array math will only look at elements in the same location (th(1) to Io(1), not th(1) to Io(2)). If you want to compare all elements of th and all elements of Io, then I suggest trying the following outside of the function:
[TH,IO] = meshgrid(th,Io);
c=cat(2,TH',IO');
d=reshape(c,[],2);
When you run the function with this, just pass d(:,1) as th input, and d(:,2) as Io input.

Show 1 older comment
Bob Nbob on 25 Jun 2019
You can enter whatever you would like into th and Io, I just chose [1 2 3] as an example. The only restriction is they need to be numeric values.
'As for putting th and Io into the function, how would I put those ranges or the output of the for loop into the function?'
I don't understand what you're asking with this.
Noah Wilson on 25 Jun 2019
I understand how you got to the range and the loop but I am lacking the understanding of how to implement that into the existing code to get the same results as before. Sorry for the confusion.
Bob Nbob on 25 Jun 2019
To get the same result as before, set th = [132.8]; and Io = [4.1e+8];
If you want it setup for user input they have access to those values outside of the function and can change them accordingly, or you can set it up as a input prompt as well, if you would like.
th = input('Enter the th values: ');
Io = input('Enter the Io values: ');

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by