Loop for function variable

Asked by Brian about 11 hours ago
Latest activity Commented on by Brian about 3 hours ago

Hello,

I need to set up a loop which will return the answers of a different function. What is the syntax to call the variables different names d1, d2, d3 from each iteration? I have tried the code in the loop A1, A2, A3 article, and other code. It doesn't seem to be relevant. The code is:

if i == 1;

     xc = gd(2, i);
     yc = gd(3, i);
     r = gd(4, i)
%         d1->..
%     dx = dCircle(P,xc,yc,r) % x =1, 2,3 etc

It's proably something simple. Thanks for your time

0 Comments

Brian

Tags

Products

No products are associated with this question.

1 Answer

Answer by Shashank about 10 hours ago
Accepted answer

I am going to refer you back to the article you mentioned because the simple answer to your question is: don't do it.

http://www.mathworks.com/matlabcentral/answers/143

If you necessarily have to do it, then I will ask you to take a look at the EVAL function which can be used to do that.

Is there a reason you want to do that and not use the alternate approaches in the above link?

3 Comments

Brian about 6 hours ago

Cheers for the reply. The reason I want to do it this way is because it has to fit into existing code, which takes d1, d2, d3 etc already. I am new to matlab so it could take a while to change the other code. I don't mind if this part of the code is slow as it will only run once. I looked into eval, it returned numbers as the answer (unless I was doing it wrong).

Shashank about 5 hours ago

This should help:

>> i = 3;  
>> eval(['x' num2str(i) '=5'])
>> x2 =
       5
Brian about 3 hours ago

You legend! Thank you sir

Shashank

Contact us