setting y axis to specific values

34 views (last 30 days)
Ines Shekhovtsov
Ines Shekhovtsov on 4 Jan 2023
Commented: Voss on 6 Jan 2023
UPDATE FROM MY QUESTION BELOW: I should have done a bit of research before posting. Don't know why i didn't assume matlab would have an automatic way to link axes. I have used the linkaxes function to link the left and right axes together and it worked. However, i still would like to apply the (-50,50) as my standard y min and y max. So for example i have the billateral muscles that are linked and have y min and ymax values as (-20,20).
I have a tiled plot and the two plots in question are tiles 1 and 2 with corresponding ax1 and ax2 variables:
% Tile 1
ax1=nexttile
p1=plot(ChannelTitle,LUTbandpass);
xlim([xlim1 xlim2])
% Tile 2
ax2=nexttile
p2=plot(ChannelTitle,RUTbandpass);
xlim([xlim1 xlim2])
linkaxes([ax1 ax2],'y')
The column vectors LUTbandpass and RUTbandpass are what i want to find the max and min of and figure out if either is below the +/- 50 and if so, want to set the y axis to (-50,50)
Hope that makes sense.
Thank you
END EDIT
Hi,
I am curious if my below code can be made more efficent/streamlined. I have two EMG plots from a muscle, one from the right side and one from the left. I want my code to look at the y max and min of both plots and determine which is higher and then set that scale for both plots.
I currently have this for the y max and y min determination:
%ymax
if max(left)<50 && max(right)<50
ymax=50
elseif max(right)>max(left)
ymax=max(right)+.15*(max(right))
else
ymax=max(left)+.15*(max(left))
end
%ymin
if min(left)>-50 && min(right)>-50
ymin=-50
elseif min(right)<min(left)
ymin=min(right)+.15*(min(right))
else
ymin=min(left)+.15*(min(left))
end
I have a set of 8 billateral muscles and don't want to repeat this code over and over.
Once i have the ymax and ymin values i will use them in my overall plot with code:
ylim([ymin ymax])
Any help would be apprecated!
Thank you in advance
  2 Comments
Ines Shekhovtsov
Ines Shekhovtsov on 4 Jan 2023
Edited: Ines Shekhovtsov on 4 Jan 2023
An update, i should have done a bit of research before posting. Don't know why i didn't assume matlab would have an automatic way to link axes. I have used the linkaxes function to link the left and right axes together and it worked. However, i still would like to apply the (-50,50) as my standard y min and y max. So for example i have the billateral muscles that are linked and have y min and ymax values as (-20,20).
I have a tiled plot and the two plots in question are tiles 1 and 2 with corresponding ax1 and ax2 variables:
% Tile 1
ax1=nexttile
p1=plot(ChannelTitle,LUTbandpass);
xlim([xlim1 xlim2])
% Tile 2
ax2=nexttile
p2=plot(ChannelTitle,RUTbandpass);
xlim([xlim1 xlim2])
linkaxes([ax1 ax2],'y')
The column vectors LUTbandpass and RUTbandpass are what i want to find the max and min of and figure out if either is below the +/- 50 and if so, want to set the y axis to (-50,50)
Hope that makes sense.
Thank you
Voss
Voss on 6 Jan 2023
@Ines Shekhovtsov: Have you seen my answer? Do you think it will work for you? Is there anything that doesn't make sense about it?

Sign in to comment.

Accepted Answer

Voss
Voss on 4 Jan 2023
Edited: Voss on 4 Jan 2023
This is streamlined somewhat:
%ymax
max_left = max(left);
max_right = max(right);
if max_left < 50 && max_right < 50
ymax = 50;
else
ymax = 1.15*max(max_right,max_left);
end
%ymin
min_left = min(left);
min_right = min(right);
if min_left > -50 && min_right > -50
ymin = -50;
else
ymin = 1.15*min(min_right,min_left);
end
If you don't want to have these several lines of code repeated several times in your code, you can make it into a function ...
function out = get_ylim_from_data(left,right)
%ymax
max_left = max(left);
max_right = max(right);
if max_left < 50 && max_right < 50
ymax = 50;
else
ymax = 1.15*max(max_right,max_left);
end
%ymin
min_left = min(left);
min_right = min(right);
if min_left > -50 && min_right > -50
ymin = -50;
else
ymin = 1.15*min(min_right,min_left);
end
out = [ymin ymax];
end
... and call it when you need it:
ylim(get_ylim_from_data(left,right))
  2 Comments
Ines Shekhovtsov
Ines Shekhovtsov on 6 Jan 2023
Thank you for the answer, it makes sense but im just not as familiar with using functions in matlab. I will take a look at this and try it out!
Voss
Voss on 6 Jan 2023
You're welcome!
Try it out, and let me know if you have any questions or problems.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!