🎉Community Raffle - Win $25

An exclusive raffle opportunity for active members like you! Complete your profile, answer questions and get your first accepted badge to enter the raffle.
Join and Win

Loop over cell content possible?

User: "ChristianKehrer"
Altair Community Member
Updated by ChristianKehrer

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

Find more posts tagged with

Sort by:
1 - 6 of 61
    User: "L Moretti"
    New Altair Community Member
    Updated by L Moretti

    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

    User: "ChristianKehrer"
    Altair Community Member
    OP
    Updated by ChristianKehrer

    Thank you very much, Lorenzo!

    Best

    Christian

    User: "Altair Forum User"
    Altair Employee
    Updated by Altair Forum User

    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

     

    User: "RoKet"
    Altair Employee
    Updated by RoKet

    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

    User: "L Moretti"
    New Altair Community Member
    Updated by L Moretti

    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

    User: "Altair Forum User"
    Altair Employee
    Updated by Altair Forum User

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