Loop over cell content possible?

ChristianKehrer
ChristianKehrer Altair Community Member
edited October 2020 in Community Q&A

Dear script experts,

 

Do you know, if there is a way (e.g. by using cells) to write strings into a kind of 'matrix' to use in a for loop afterwards. The idea behind is to make many different plots, which are always stored under different names and then have different titles.

Maybe, someone has already done something similar?

 

Thank you in advance!
Christian

Tagged:

Answers

  • L Moretti
    L Moretti New Altair Community Member
    edited May 2019

    Hi Christian,

     

    maybe this 8 lines of code can help you:

     

    clc,clear,close all
    plotnames = cell(4,4);
    for ii=1:size(plotnames,1)
        for jj=1:size(plotnames,2)
            plotnames(ii,jj) = ['plot, row ',num2str(ii),' & column ',num2str(jj)];
        end
    end
    plotnames

     

    Cheers,

     

    Lorenzo

  • ChristianKehrer
    ChristianKehrer Altair Community Member
    edited May 2019

    Thank you very much, Lorenzo!

    Best

    Christian

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited May 2019

    Thanks for this Skript.

    One more Question. Now we have a kind of cell Matrix.

     

    Is it possible to use this cell Matrix for labeling my Axis in a plot.

    I thougt this should work, but the solver Needs a 'real' String.

     

    f=plot(x,y);

    xlabel(Plotnames(1,1));

    ylabel(Plotnames(1,2));

     

    Would be great if someone worked with this Problem bevore.

    Thanks

    Ludwig

     

  • RoKet
    RoKet
    Altair Employee
    edited June 2019

    Hi Ludwig,

    You can convert the cell into a struct using cell2struct, but I think it is too cumbersome.

    An easier way is to use directly a struct with the limitation of having only one dimension, but named fields. The script looks lke this:

     clc,clear,close all  for i = 1 : 4     plotnames(i).title = ['plot, title ',num2str(i)];     plotnames(i).xlabel = ['xlabel ',num2str(i)];     plotnames(i).ylabel = ['ylabel ',num2str(i)]; end  % direct access or within a loop title(plotnames(3).title); xlabel(plotnames(3).xlabel); ylabel(plotnames(3).ylabel);

    Regards,
    Ronald

  • L Moretti
    L Moretti New Altair Community Member
    edited June 2019

    Hi Ludwig,

     

    I believe that the solution suggested by Ronald it's great.

     

    Anyway, this should fix the error you were experiencing:

     

    xlabel(Plotnames{1,1});

    ylabel(Plotnames{1,2});

     

    I have just replaced () with {}.

     

    Cheers,

     

    Lorenzo

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2019

    Thanks for this help, the path you two showed me is verry efficient.