How to set YLim for multiple Yaxis on UIaxes

1 view (last 30 days)
I am plotting a figure using uiaxes in app designer with 2 y axis one on the left and other on the right. And I am trying to set YLim for both left and right. But the code I wrote was wrong. The one I right for the right is overwriting the one on the left. So technically I am only setting YLim for the left one.
yyaxis(app.UIAxes_4,'left');
plot(app.UIAxes_4,app.actualTime,app.Charge_Current);
app.UIAxes_4.XMinorGrid ="on";
app.UIAxes_4.YMinorGrid ="on";
app.UIAxes_4.YLabel.String = 'Current [A]';
app.UIAxes_4.YLabel.FontSize = 12;
app.UIAxes_4.YLabel.FontWeight ="bold";
app.UIAxes_4.YLim = [app.Y1min app.Y1max]; % This sets the YLim correctly for the left side
yyaxis(app.UIAxes,'right');
plot(app.UIAxes,app.actualTime,voltage);
app.UIAxes.XMinorGrid ="on";
app.UIAxes.YMinorGrid ="on";
app.UIAxes.YLabel.String = 'Current [A]';
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight ="bold";
app.UIAxes_4.YLim = [app.Y2min app.Y2max]; % This overrides the Ylim set earlier and sets a new YLim for
% left side without setting
% Ylim for the right side.
What is the way to set Ylim on both left and right. Thank you.
  4 Comments
Shivam
Shivam on 11 Sep 2024
Could you attach your app for me to debug the issue? Use the paperclip icon to attach it.
Govind Sankar Madhavan Pillai Ramachandran Nair
no I cant attach the app, because there are confidential details in the app. Anyway I just need to know how to set YLim on both Y axis on left and right. How can I do that?

Sign in to comment.

Answers (2)

Jatin
Jatin on 11 Sep 2024
Edited: Jatin on 12 Sep 2024
The line 'yyaxis(app.UIAxes, 'right')' activates the right y-axis for the specified parent, which is app.UIAxes.
Since you haven't used 'yyaxis(app.UIAxes_4, 'right')', your attempt to set the YLim for the right y-axis of app.UIAxes_4 is actually applied to the left y-axis in the last line, as it remains the active side for the app.UIAxes_4 axes.
Refer to the documentation below to learn more about the 'yyaxis':
Hope it helps!
  2 Comments
Govind Sankar Madhavan Pillai Ramachandran Nair
Moved: Walter Roberson on 12 Sep 2024
Omg, I didnt see that, I am so sorry. That was really stupid. And thank you very much.

Sign in to comment.


nick
nick on 11 Sep 2024
Hi Govind,
I understand that you want to create a chart with two y-axes each having separate limits.
When the right side of the UI axes is activated using the function 'yyaxis' , it acts upon the object 'app.UIAxes' instead of the 'app.UIAxes_4.YLim' object. This causes the right side of the 'app.UIAxes_4.YLim' object to remain inactive and causes the right to overwrite the one on the left. Here's a sample MATLAB script that implements the two y-axes with different limit:
time = linspace(0, 10, 100); % Time from 0 to 10 seconds
current = sin(time); % Dummy current data
voltage = 10*cos(time); % Dummy voltage data
% Plot on the left y-axis
yyaxis(app.UIAxes, 'left');
plot(app.UIAxes, time, current, '-b', 'DisplayName', 'Current');
app.UIAxes.YLabel.String = 'Current [A]';
app.UIAxes.YLim = [-1.5, 1.5]; % Example Y limits for the left axis
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight = 'bold';
% Plot on the right y-axis
yyaxis(app.UIAxes, 'right');
plot(app.UIAxes, time, voltage, '-r', 'DisplayName', 'Voltage');
app.UIAxes.YLabel.String = 'Voltage [V]';
app.UIAxes.YLim = [-15, 15]; % Example Y limits for the right axis
app.UIAxes.YLabel.FontSize = 12;
app.UIAxes.YLabel.FontWeight = 'bold';
% Add grid and title
app.UIAxes.XMinorGrid = 'on';
app.UIAxes.YMinorGrid = 'on';
title(app.UIAxes, 'Current and Voltage vs. Time');
xlabel(app.UIAxes, 'Time [s]');
legend(app.UIAxes, 'show');
You can refer to the following documentation to learn more about the function 'yyaxis':
Hope this helps.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!