Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Second y scale for plotting within a loop

Subject: Second y scale for plotting within a loop

From: Florian

Date: 10 Jul, 2012 09:25:09

Message: 1 of 5

Dear MatLab experts!

I have a problem with plotting a second y scale when I try to do this within a loop. At the moment I use a user input to control the columns of a matrix that get plotted within a loop.
Now I would like to plot the LAST TWO COLUMNS in an additional y scale. At the moment I only use one y scale. However, a second y scale would be more suitable and that’s why I’m scrambling for another solution.
I’m aware of the ‘plotyy’ command but I haven’t found a way to use it for my scenario yet (plotting within the loop). Maybe you could help me on this matter :-)

%I left out some less important code. Here the things you need to know:
%In „rawdata“ there is all the data and the respective column headers. The first column always includes the time information of my measurement data. So it’s always my x scale.
%The variables like „schwarz, brau,…“ contain information to colour the plotted data

eingabe=input('Select the columns you want to plot (For example: 2,9,10): ','s');
a=textscan(eingabe,'%d','delimiter',',');
auswahl=a{:};
jj=(length(auswahl));

%Now the code that needs to be changed
% At the moment I plot the selected columns within a loop BUT ACTUALLY I WANT TO PLOT THE LAST TWO COLUMNS IN A SECOND SCALE!!!!

%---------------------------- Plot -----------------------------------
%refresh
figure(1)
hFig=figure(1);
set(hFig,'PaperPositionMode','Auto','Position',[0 0 1200 700]);
hold on;
txt_legend='';
for i = 1:(length(auswahl))
    p_i=plot(rawdata(:,1),rawdata(:,auswahl(i)));
    switch wahl(i)
        case 1
            col=schwarz;
        case 2
            col=braun;
        case 3
            col=grau;
        case 4
            col=dgruen;
        case 5
            col=hgruen;
        case 6
            col=dblau;
        case 7
            col=hblau;
        case 8
            col=dvio;
        case 9
            col=hvio;
        case 10
            col=rot;
        case 11
            col=orange;
        otherwise
            col=schwarz;
    end
    set(p_i,'Color',col,'LineWidth',1.5)
    txt_legend=[txt_legend colheaders{auswahl(i)-1} ':'];
end

txt_legend;
real_legend=textscan(txt_legend,'%s','delimiter',':');
titel=input('Chose title: ','s');
besch=title(titel);
set(besch, 'FontSize',12, 'FontWeight','bold');
xlabel('Time [*10ms]')
ylabel('sensordata')
leg=legend(real_legend{:});
set(leg, 'FontSize',8);
set(legend,'Location','NorthEastOutside')
grid on
%grid(gca,'minor')
hold off;
%--------------------------------------------------------------------------


Hopefully I could describe my problem clearly.
Thanks in advance for all the help!!!

Subject: Second y scale for plotting within a loop

From: dpb

Date: 10 Jul, 2012 13:36:28

Message: 2 of 5

On 7/10/2012 4:25 AM, Florian wrote:
...
> Now I would like to plot the LAST TWO COLUMNS in an additional y scale.
> At the moment I only use one y scale. However, a second y scale would be
> more suitable and that’s why I’m scrambling for another solution.
> I’m aware of the ‘plotyy’ command but I haven’t found a way to use it
> for my scenario yet (plotting within the loop). Maybe you could help me
> on this matter :-)
...

There's an example of using multiple axes at

<http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html>

You could also likely use plotyy() first (saving the handles of course)
and hide the right axis plot until wanted I'd think if the order is
really that significant.

Or, "all" plotyy() is is a wrapper around a double axis graph similar to
the example--so you might as well do that :)

--

Subject: Second y scale for plotting within a loop

From: Florian

Date: 10 Jul, 2012 14:03:09

Message: 3 of 5

Thank you dpb!

I was just trying to describe my problem in a different way because nobody replied on my initial post :)

Here is another brief overview of my issue:
I want to plot 6 columns of a matrix. I need two different y-axes and that was why I thought of using the plotyy command. I want the data of 4 columns on the “left” y-axis and the data of the other 2 columns on the “right” y-axis.

hold on
[ax1, h1, h2] = plotyy(time,col1, time, col6);
[ax2, h3, h4] = plotyy(time, col2, time, col5);
plot3 = plot(time, col3);
plot4 = plot(time, col4);
hold off;

The problem is that I would have a lot of handles. This makes possible changes to limits, ticks or whatever very troublesome. So the solution is not very comfortable.
Does someone have a better idea or am I using the plotyy in a wrong way?

@dpb: On the webpage you sent me the link to was the hint “If your data shares an x-axis, you can also use the plotyy function…”
So it seems that plotyy is the way to go. I just haven’t figured the right way to do so ;)

Subject: Second y scale for plotting within a loop

From: dpb

Date: 10 Jul, 2012 15:51:23

Message: 4 of 5

Thank you dpb!

I was just trying to describe my problem in a different way because
nobody replied on my initial post :)

Here is another brief overview of my issue:
I want to plot 6 columns of a matrix. I need two different y-axes and
that was why I thought of using the plotyy command. I want the data of 4
columns on the “left” y-axis and the data of the other 2 columns on the
“right” y-axis.

hold on
[ax1, h1, h2] = plotyy(time,col1, time, col6);
...

Only use plotyy() once--after that, add data to the existing two axis
handles, don't keep making new ones.

@dpb: On the webpage you sent me the link to was the hint “If your data
shares an x-axis, you can also use the plotyy function…”
So it seems that plotyy is the way to go. I just haven’t figured the
right way to do so ;)

Hmmm....don't understand that--it opens a page w/ the following

> Using Multiple X- and Y-Axes
> On this page…
>
> Introduction
>
> Example — Double Axis Graphs

when I paste that link into the browser.

In Contents it's under Users Guide:Graphics:Using Axes Objects:Using
Multiple X- and Y-Axes

--

--

Subject: Second y scale for plotting within a loop

From: Florian

Date: 11 Jul, 2012 14:58:13

Message: 5 of 5

Thanks again!

After you suggestion I found this:
http://stackoverflow.com/questions/9129565/choosing-axis-in-plotyy

Now I could create solutions that works out pretty fine.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us