Clear Filters
Clear Filters

Saving a figure in matlab with zoom functionality

15 views (last 30 days)
I have a code that generates a plot of my original signal, the rectified signal and then a liner envelpe of the signal. I would like to be able to zoom in on any plot(currently i think my code sets it to the original but if it can be modified to be any plot then if anyone knows how to do that i would apprecate it) and have the other two plots zoom in to the same point automatically. My below code currently does tht if you zoom in on the original signal. I wish to save the figure and then have that zoom funcitonality still be active for the figure. I tried a few things but they didn't work. I'm not very familiar with matlab but i imagine that for it to have the zoom funcitonality you would need to run a script to open the figure with said functionality. If anyone can help out with that part of the code, it would be greatly apprecated. Thank you for your time in advance!
% Plot the original signal, rectified signal, and smoothed signal
figure;
h1 = subplot(3,1,1);
plot(t, original_signal);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
h2 = subplot(3,1,2);
plot(t, rectified_signal);
title('Rectified Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
h3 = subplot(3,1,3);
plot(t, smoothed_signal);
title('Linear Envelope');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Set the same x-axis limits for all subplots
common_xlim = [min(t), max(t)]; % Set x-axis limits to min and max of original signal
set(h1, 'XLim', common_xlim);
set(h2, 'XLim', common_xlim);
set(h3, 'XLim', common_xlim);
% Add zoom callback to the original signal subplot
hZoom = zoom;
hZoom.ActionPostCallback = @(obj,event_obj) set([h1,h2,h3],'XLim',get(event_obj.Axes,'XLim'));

Accepted Answer

Ranjeet
Ranjeet on 11 Apr 2023
Hi Ines,
After testing the provided code in MATLAB R2022b, following code can be tested to zoom in all the subplots when any specific subplot is zoomed in a figure.
% get some random integer vectors for original, rectified and smoothed
% signal
original_signal = randi(100, 500, 1);
rectified_signal = randi(75, 500, 1);
smoothed_signal = randi(25, 500, 1);
t = 1:500;
% Plot the original signal, rectified signal, and smoothed signal
figure;
h1 = subplot(3,1,1);
plot(t, original_signal);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
h2 = subplot(3,1,2);
plot(t, rectified_signal);
title('Rectified Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
h3 = subplot(3,1,3);
plot(t, smoothed_signal);
title('Linear Envelope');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Set the same x-axis limits for all subplots
common_xlim = [min(t), max(t)]; % Set x-axis limits to min and max of original signal
set(h1, 'XLim', common_xlim);
set(h2, 'XLim', common_xlim);
set(h3, 'XLim', common_xlim);
zoom on;
% Link the x-axis of the subplots
linkaxes([h1 h2 h3],'x')
To get more information on linkaxes, you can refer the following link.
  1 Comment
Ines Shekhovtsov
Ines Shekhovtsov on 11 Apr 2023
Thank you for your help. Can you save the figure and have the zoom functionality still be active or must a seperate smaller script be run? And if so, can you help me with what that script might be.

Sign in to comment.

More Answers (0)

Categories

Find more on Two y-axis 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!