plotyy in for loop only plots half of the data

Hi guys,
i don't know what to do further. I have this problem since 3 days and i just can't find a solution. I want to plot 10 graphs on each axis in a for loop. The first 10 are working perfectly fine with legend and colours. For some reason (hold?) the second axis only plots the last one.
Here is my code:
if true
%code
for zaehlerfigImp=1:anzFigImp
%Ploteinstellungen
YACHSENTITEL='Z''''';
XACHSENTITEL='Z''';
FontSizeTitle=20;
FigureImp=figure(zaehlerfigImp);
set(FigureImp, 'Position', [50 100 800 400])
%Schleifendurchlauf, der immer die Anzahl der vorher definierten Graphen in
%eine Figure plottet
for p=1:laengeReihe; %Plotten aller gefundenen Dateien mit Dateiname als Legende
subplot(211)
plot(Datenbank(:,uebersichtzaehler+zaehlerplot+p), Datenbank(:,uebersichtzaehler+zaehlerplot+(p+1)), 'Color',Colour(p*200,:), 'LineWidth',2,'DisplayName',[Uebersicht{uebersichtzaehler+p,1} ' ' Uebersicht{uebersichtzaehler+p,2} ' ' Uebersicht{uebersichtzaehler+p,3} ' ' Uebersicht{uebersichtzaehler+p,4} ' ' Uebersicht{uebersichtzaehler+p,5}]);
grid on
legend('-DynamicLegend');
set(gca,'YDir','reverse'); % Y-Achse invertieren
ylabel(YACHSENTITEL, 'FontSize',15)
xlabel(XACHSENTITEL, 'FontSize',15)
hold on;
subplot(212)
zbetrag(:,p)=sqrt((Datenbank(:,uebersichtzaehler+zaehlerplot+p)).^2+(Datenbank(:,uebersichtzaehler+zaehlerplot+(p+1))).^2);
hilfsvarphase(:,p)=(Datenbank(:,uebersichtzaehler+zaehlerplot+(p+1)))./(Datenbank(:,uebersichtzaehler+zaehlerplot+p));
phase(:,p)=atand(hilfsvarphase(:,p));
[AX,H1,H2]=plotyy(f_bereich(:,1),zbetrag(:,p),f_bereich(:,1),phase(:,p),'plot','plot');
grid on
hold all
set(H1,'DisplayName', [ 'Z:' Uebersicht{uebersichtzaehler+p,2} ' ' Uebersicht{uebersichtzaehler+p,3} ' ' Uebersicht{uebersichtzaehler+p,4} ' ' Uebersicht{uebersichtzaehler+p,5}])
set(H2, 'DisplayName' , [ 'Phase:' Uebersicht{uebersichtzaehler+p,2} ' ' Uebersicht{uebersichtzaehler+p,3} ' ' Uebersicht{uebersichtzaehler+p,4} ' ' Uebersicht{uebersichtzaehler+p,5}])
set(get(AX(1),'Ylabel'),'String','Z')
set(get(AX(2),'Ylabel'),'String','Phase')
xlabel('Frequenz','FontSize',FontSizeTitle)
hold all;
legend('-DynamicLegend');
zaehlerplot=zaehlerplot+1;
hold all;
end
set(AX,'XScale','log');
set(AX(1),'Box','off');
legend('location', 'SouthEast'); % Legende günstig platzieren
%title(['Impedanzspektren von:' ' ' Uebersicht{uebersichtzaehler+p,2} ' mit ' Uebersicht{uebersichtzaehler+p,3}] ); %Bei Bedarf Diagrammtitel
hold off;
set(gcf,'Name',['Imp: ' Uebersicht{uebersichtzaehler+p,2} '_' Uebersicht{uebersichtzaehler+p,3}]);
%Abspeichern der Figure als jpg
%print( FigureImp, '-djpeg' , ['diagramme\' 'imp_' Uebersicht{uebersichtzaehler+p,2} '_' Uebersicht{uebersichtzaehler+p,3} ]);
%print( FigureImp, '-depsc2' , ['diagramme\' 'imp_' Uebersicht{uebersichtzaehler+p,2} '_' Uebersicht{uebersichtzaehler+p,3} ]);
uebersichtzaehler= uebersichtzaehler+laengeReihe;
end
end
The problem is only the part in subplot212. everything else is working fine...
I'm in desperate need of help! :(
[EDITED, Jan] Data file: Testdata.mat

Answers (3)

Hi,
Your data are not defined in the code you show, so I can't run it and just can to guess what the problem might be.
Did you try to run this code in debug mode ? Put a breakpoint at the line
[AX,H1,H2]=plotyy(f_bereich(:,1),zbetrag(:,p),f_bereich(:,1),phase(:,p),'plot','plot')
and see if the plot is well created or erased, or ...
and just to be sure, are all the columns of zbetrag and phase different from each other ? otherwise you will always plot the same values.
Hi,
thank you for trying to help me. I have a huge Database, the Data is correct. I testet manually typing more plotyy's with hold all between it, and that works fine! The Data ist not the same, and i can see, that the script is plotting all 10 plots, because the legend that appears at the end is the one from the last graph.
so i guess somehow, the hold all in a for loop doesn't catch the second axis..
would it help if i send you an .m file with a few datas to plot?

4 Comments

it will sure be easier to test with the a mfile attached to your question.
Sebastian
Sebastian on 16 Oct 2014
Edited: Sebastian on 16 Oct 2014
let me know, if anything is missing
i did a debug, anytime the plotyy is plotting a new set of data, the set of the second axis is deleted before that, while the first axis stays untouched..
it misses initial values of uebersichtzaehler and zaehlerplot
just set them 0 on top of the code.

Sign in to comment.

I think the use of plotyy is not appropriate in your case. you usually use it to plot only two data with high amplitude difference. but here you want to combine 10*2 set of datas in the same plot which probably will be really hard to read.
can't you use two different subplot to plot zbetrag and phase, which will be kind of "cleaner" ?
see attached file test_file2.m.

4 Comments

yeah, i have to admit my head is so confused right now i didn't even think of that. Think I'll go that way, but it still bothers me not to find a solution or a reason for that let's say "bug" :/
my guess is that plotyy is not made to suit multiple plots, nor even to support legend (there is no example using legend in the doc page of plotyy). in your original code, you plot 20 data in the 2nd subplot, but the legend contains only 10 strings.
I saw codes with more than 2 plots in an plotyy. their problem was the legend, i fixed the legend, it always reads the string out of my "Uebersicht" with the index of "p", so there are 20 strings, because it's always generated in the loop.
but anyway, there is no disadvantage at this point in using 2 subplots.
@Sebastian: plotyy and subplot do almost the same: They create two axes objects. The only difference is the smart positioning in subplot and that in plotyy the axes objects have the same position, but different locations of the YTicks.

Sign in to comment.

Categories

Asked:

on 16 Oct 2014

Edited:

Jan
on 25 Oct 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!