How to use scrollsubplot in a panel in app designer?

7 views (last 30 days)
I have a GUI with two panels. One panel is filled with a number of plots after pushing a button. The number of plots can vary, but when this number increases the plots get very squished together and hard to read. I thought I could work around this using the scrollsubplot() function from file exchange, but each time the button is pressed a separate figure is opened. The plots populate the correct areas in the panel, but the scrollbar is located in this new figure. Here is a portion of my code:
PlotInd = 1;
axBrainStO2 = scrollsubplot(4,1,PlotInd);
axBrainStO2.Parent = app.RightPanel;
plot(axBrainStO2,app.timeNIRS,app.BrainStO2,'m-','LineWidth',1.5)
Is there any way to fix this?
  4 Comments
Dolon Mandal
Dolon Mandal on 20 Sep 2023
Instead of using the scrollsubplot function, you can create a single axes object within your panel and update its position dynamically based on the number of plots. Here's an example of how you can modify your code:
% Assuming you have a GUI with a panel named 'RightPanel'
% Calculate the number of rows and columns based on the number of plots
numPlots = 4; % Replace with your actual number of plots
numRows = ceil(sqrt(numPlots));
numCols = ceil(numPlots / numRows);
% Calculate the position of the axes within the panel
plotInd = 1; % Replace with the appropriate plot index
plotPosition = [mod(plotInd-1, numCols)/numCols, floor((numRows-1-(plotInd-1)/numCols))/numRows, 1/numCols, 1/numRows];
% Create the axes within the panel
axBrainStO2 = axes('Parent', app.RightPanel, 'Position', plotPosition);
% Plot the data on the axes
plot(axBrainStO2, app.timeNIRS, app.BrainStO2, 'm-', 'LineWidth', 1.5);
In this modified code, we calculate the number of rows and columns based on the desired layout of the plots. Then, we calculate the position of each plot within the panel using the plot index and the number of rows and columns. The plotPosition variable represents the position of each plot as a normalized value between 0 and 1.
Next, we create an axes object within the panel using the axes function and set its position using the Position property. The Parent property is set to the RightPanel to ensure that the axes is embedded within the panel.
Finally, we plot the data on the axes using the plot function.
You can repeat this code for each plot, updating the plotInd and plotPosition variables accordingly.
With this approach, all the plots will be contained within a single axes object, and you can control their positions and layout within the panel.

Sign in to comment.

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!