How to legend two curves in a single plot line, MATLAB R2022a App designer.

12 vues (au cours des 30 derniers jours)
Jorge Mauro Ballester Córdoba
Commenté : Matt J le 2 Juin 2022
Hi there!
I am having a big trouble trying to write the code lines to legend my plot in App Designer.
My app is meant to read the text files when I select a folder and then I pick the files I want to plot from a Listbox. Since here the code works wonders. When it comes to the plot, I need to pass the func yyaxis to UIAxes to see a 2nd Y label. I need to plot 2 curves for every text file I read, so I represent both in one single plot (1 line of code) using the same color for the 2 lines of a file, but different marker shapes.
I am struggling with the legend bc I don't know how to use legend func to couple 2 marker shapes for each text file name (stored in app.value{}). Instead, I am getting the last marker shape and the last text file name I plot. I'll show you the code I wrote to clarify any doubts.
Any help would be really appreciated! Thanks in advanced.
Switch case...
case 'Bode - Re & Phase vs Fq'
yyaxis(app.UIAxes,'left');
app.UIAxes.XScale = 'log';
app.UIAxes.YDir = 'normal';
for i = 1:size(app.value,2)
plot(app.UIAxes, FrequencyHz{i}, impedanceOhm{i}, '--', FrequencyHz{i}, Phasedeg{i}, ...
':.', 'color', C{i}, 'LineWidth', 2)
hold(app.UIAxes, "on");
end
hold(app.UIAxes, "off");
legend(app.UIAxes, strrep(app.value{i},'_',' '))
xlabel(app.UIAxes, 'Frecuencia (Hz)');
ylabel(app.UIAxes, 'Impedancia (Ohm)');
title(app.UIAxes, strcat('Bode EIS - Impedancia & Fase'));
ylabel(app.UIAxes,'Grados (º)');

Réponses (1)

Matt J
Matt J le 1 Juin 2022
Modifié(e) : Matt J le 1 Juin 2022
case 'Bode - Re & Phase vs Fq'
yyaxis(app.UIAxes,'left');
app.UIAxes.XScale = 'log';
app.UIAxes.YDir = 'normal';
for i = 1:size(app.value,2)
plot(app.UIAxes, FrequencyHz{i}, impedanceOhm{i}, '--', FrequencyHz{i}, Phasedeg{i}, ...
':.', 'color', C{i}, 'LineWidth', 2)
hold(app.UIAxes, "on");
end
hold(app.UIAxes, "off");
legText=replace(app.value,'_',' ');
legend(app.UIAxes, legText{:});
  2 commentaires
Jorge Mauro Ballester Córdoba
Hi Matt! Thanks for answering!
Your code make legend show every text file name but Im afraid sth is still wrong. As you can see in the plot line, for every text file I plot 2 curves with 2 marker shapes (one for each) with the same color. Therefore, the legend should show 2 shapes with the same colour and the same file name. Instead, I got a shape with a file name and the other shape with another file name, also just one color.
Hope we can fix this.
Matt J
Matt J le 2 Juin 2022
How about with,
legText=repelem( string( replace(app.value,'_',' ') ) ,2);

Connectez-vous pour commenter.

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by