how to access string declared variables in scilab

257 views Asked by At

I am having trouble with is line "P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);" basically I have declared matrix P_dot_ij_om_1=[] from 1 to i and in the next line.

I would like to input data e.g. P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2); where ii is a number. what is the right expression.


rows=round(k/360);
i=1;
ii=1;
k=1;
while ii <= rows

     Pdot_names(ii,1)=string("P_dot_ij_om_"+ string(ii));

     disp(Pdot_names(ii))
     execstr(Pdot_names(ii)+'=[]');         // declare indexed matrix

     while i<361

        P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);
        // P_dot_ij_om_+ii(i,2)=P_dot_ij(k,3);

         disp(k)
         k=k+1;
         i=i+1;
     end
ii=ii+1;


end

1

There are 1 answers

0
Attila On

The code below works, but in general it is not advised to create string variables. There are much faster and also easier to implement alternatives, see e.g. this thread: How can I create variables Ai in loop in scilab, where i=1..10

rows = 2;
P_dot_ij = [10,20,30;11,21,31;12,22,32];
i=1;
ii=1;
k=1;
while ii <= rows

    //Pdot_names(ii,1)=string("P_dot_ij_om_"+ string(ii));    The firs 'string' is unnecessary
    Pdot_names(ii,1)="P_dot_ij_om_"+string(ii);    

    disp(Pdot_names(ii))
    execstr(Pdot_names(ii)+'=[]');         // declare indexed matrix

    while i<4

        //P_dot_ij_om_+ii(i,1)=P_dot_ij(k,2);
        execstr("P_dot_ij_om_"+string(ii)+"("+string(i)+",1)=P_dot_ij("+string(k)+",2)");           // P_dot_ij_om_+ii(i,2)=P_dot_ij(k,3);
        execstr("P_dot_ij_om_"+string(ii)+"("+string(i)+",2)=P_dot_ij("+string(k)+",3)");

        disp(k)
        k=k+1;
        i=i+1;
    end
    ii=ii+1;
end

disp(P_dot_ij_om_1,"P_dot_ij_om_1");
disp(P_dot_ij_om_1,"P_dot_ij_om_2");

And also next time please post selfcontained code examples, because otherwise I can only guess what is k and P_dot_ij.