Working with different variables in Cycle

2 views (last 30 days)
Hi i have question.
Basically i have a for cycle and in every iteration I would like to use different Variable.
I have for example Variable C which is matrix and I want to use first column and all rows so C(:,1). I have several variables with different names like D1, D2, T1, T2 and so on.
So in the first iteration i=1 I want to use variable C and in i=2 variable D2 and so on.
Now I looking for a way how to manage it. I tried some cell2mat but it didn't work.
Part of the code I have:
Par = cell2mat(Param(i)) % In Param cell i have stored names of variables where Param(1)=C and Param(2)=D1 for example
ind = find(P_VB(1,1)==Par(:,1)); %Now i would like from matlab to substitute Par with C is it would be ...C(:,1);
Hope it makes sense what i want to achieve.
Thanks a lot.

Accepted Answer

Jan
Jan on 26 Apr 2012
Using D1, D2, ... als variables must lead to such problems, see: FAQ: Create A1, A2, ... in a loop. And if you have loaded the FAQ already, take the time to read the other topic also.
Better use the cell array D{1}, D{2}, ... Then the variables are easy to access inside a loop.

More Answers (1)

madik
madik on 26 Apr 2012
Yes this helped me to resolve my problem. Appreciate a lot Jan. And a brief question. I created a cell with A=cell(12,1) How can I add all my variables with one command to the array A? like A{}=[A B C D ...]
Now i have to write it one by one like: A{1}=A; A{2}=B; A{3}=C;
Thanks

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!