Error trying to plot two different graphs
Show older comments
function [] = Graphs(X1,X2)
Y1 = X1.Time;
Z1 = X1.Data;
Y2 = X2.Time;
Z2 = X2.Data;
% Create figure
figure1 = figure('WindowState','maximized');
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create multiple lines using matrix input to plot
subplot(2,1,1);
plot1 = plot(Y1,Z1,'LineWidth',2,'Parent',axes1);
set(plot1(1),'DisplayName','Vout');
%set(plot1(2),'DisplayName','Va');
%set(plot1(3),'DisplayName','Vb');
%set(plot1(4),'DisplayName','Vc');
% Create ylabel
ylabel('Voltaje(V)','FontName','Times New Roman','FontSize',12);
% Create xlabel
xlabel('Tiempo(s)','FontName','Times New Roman','FontSize',12);
% Create title
title('Tensión de Salida:','FontName','Times New Roman','FontSize',12);
box(axes1,'on');
hold(axes1,'off');
% Create legend
legend(axes1,'show');
grid;
grid minor;
%-----------------------------------Segunda Gráfica
% Create axes
axes2 = axes('Parent',figure2);
hold(axes2,'on');
% Create multiple lines using matrix input to plot
subplot(2,1,2);
plot2 = plot(Y2,Z2,'LineWidth',2,'Parent',axes2);
set(plot2(1),'DisplayName','Iout');
%set(plot2(2),'DisplayName','Ia');
%set(plot2(3),'DisplayName','Ib');
%set(plot2(4),'DisplayName','Ic');
% Create ylabel
ylabel('Corriente(A)','FontName','Times New Roman','FontSize',12);
% Create xlabel
xlabel('Tiempo(s)','FontName','Times New Roman','FontSize',12);
% Create title
title('Corriente de Salida:','FontName','Times New Roman','FontSize',12);
box(axes2,'on');
hold(axes2,'off');
% Create legend
legend(axes2,'show');
grid;
grid minor;
end
This function is supposed to graph two to wokspace simulink variables in two subplots with Graphs(out."X",out."Y") but instead it gives this error:
Error using plot
Invalid handle.
Error in Graphs (line 17)
plot1 = plot(Y1,Z1,'LineWidth',2,'Parent',axes1);
Answers (1)
Voss
on 15 Mar 2022
Calling subplot() deletes any axes already in your figure (except axes created with a previous subplot() call), so you should probably either (1) create axes with the axes() function or (2) create axes with the subplot() function, but not both.
For instance, using subplot() to create your first axes might look like this:
axes1 = subplot(2,1,1);
hold(axes1,'on');
plot1 = plot(Y1,Z1,'LineWidth',2,'Parent',axes1);
set(plot1(1),'DisplayName','Vout');
Then you can use axes1 subsequently, the same as you are doing now.
And do the same thing for axes2:
axes2 = subplot(2,1,2);
hold(axes2,'on');
plot2 = plot(Y2,Z2,'LineWidth',2,'Parent',axes2);
set(plot2(1),'DisplayName','Iout');
Then use axes2 like you are already doing, as well.
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!