Jump to content
christiankeh

Loop over cell content possible?

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...