Restore View returns the axis of an old data set when the dataset is changed.

27 views (last 30 days)
I have made an app in app designer where I have a dropdown menu to select from various sources of data. I get the user to select the data they want, and it then plots this data against a time window that is also specified by the user. I specify the y-axes limits to be the min and max values of the chosen data set within the specified time interval. I then would like to zoom into the data and then restore the view. This works fine for the first dataset. If the user then selects a different set of data from the dropdown, the plot is made with the new data and the y-axes limits are set to the new min and max values of the new dataset within the same specified time window. However, if the user zooms in and restores the view, the y-axes limits of the old dataset are returned as the y-axes limits. Does anyone have any ideas how to fix this issue?

Accepted Answer

Ayush Singh
Ayush Singh on 10 Jun 2024
Edited: Ayush Singh on 10 Jun 2024
Hey Shawn,
The issue you are encountering sounds like it is related to the way MATLAB's zoom functionality is interacting with the dynamic updating of your axes limits. When you zoom and then use the "restore view" feature, it is likely reverting to the view state (including axes limits) that was saved at the time of the initial plot or the last time the zoom tool's "save view" state was explicitly updated.
To ensure that the axes limits update correctly when you switch datasets and still allow for zooming and restoring the view to the appropriate state for the current dataset, consider the following approach:
1. Explicitly Update Zoom State After Plotting New Data:
After updating the plot with the new dataset and setting the axes limits, explicitly update the zoom tool's "save view" state to match the current view. This way, when the user restores the view after zooming, it should revert to the correct, most recently set axes limits.
2. Use Callbacks to Manage Axes Limits:
Ensure that the axes limits are updated both after initially plotting the data and after any zoom actions. You might need to use callback functions associated with the zoom actions to reset the axes limits dynamically based on the currently selected dataset.
In order to achieve the above approach you can follow below steps:
Step 1: Update Plot and Axes Limits for New Data
When you select a new dataset from the dropdown menu, you likely have a callback function that handles the plotting. After plotting the new data and setting the axes limits, add a command to update the zoom tool's state.
% Callback function for updating the plot
function updatePlot(app)
% Plot new data
plot(app.UIAxes, newDataX, newDataY); % Assuming UIAxes is your axes object and
% newDataX and newDataY are your axes values from the new dataset
% Update axes limits
app.UIAxes.YLim = [min(newDataY), max(newDataY)];
% Update zoom state
zoomObj = zoom(app.UIFigure); % Assuming UIFigure is your figure object
zoomObj.ActionPostCallback = @(obj,evd) updateAxesLimits(app, evd.Axes);
zoomObj.Enable = 'on';
end
For more information on 'zoom' function refer following documentation:
Step 2: Define a Function to Update Axes Limits Based on Current Data
You might need a dedicated function to correctly set the axes limits based on the current dataset, especially if you are allowing to zoom and then want to programmatically restore to the dataset-specific view.
function updateAxesLimits(app, axesHandle)
% Assuming you have a way to determine the current dataset's Y limits
% This might involve storing the current dataset or its limits in a property of the app
newYLim = [min(app.currentDataSetY), max(app.currentDataSetY)];
axesHandle.YLim = newYLim;
end
Step 3: Adjust Zoom Callbacks if Necessary
Depending on your app's structure and the specific user interactions, you might need to adjust how and when you attach callbacks to the zoom object, ensuring they correctly reflect the current state and data.
Ensure that `app.currentDataSetY` (or however you choose to track the current dataset or its limits) is updated every time the dataset changes.
Hope the above steps resolve your issue!

More Answers (1)

Shawn
Shawn on 2 Jul 2024
I ended up using the following code which worked.
zoom(app.UIaxes, 'reset')

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!