Create a selectable grid in a MATLAB app
15 views (last 30 days)
Show older comments
I am trying to create a MATLB app with APPDesigner. I have added an axis element that has to contain a series of subplots (16x16). I have been able to create the subplots and plot them in the app. However, I wanted to create a grid that separates the subplots, and lets the user select a specific square from the grid to then display their selection in the app. This is what I currently have:
k = 1;
axes_handle = app.SpectralPlotAxes;
figure_spectra = fiu¡gure('Visible', 'off'); % Create a new figure (not visible)
hold on;
figureWidth = 800; % Adjust the figure width as desired
figureHeight = figureWidth; % Set the figure height equal to the width to make it square
set(figure_spectra, 'Units', 'pixels');
set(figure_spectra, 'Position', [100, 100, figureWidth, figureHeight]);
for i = 1:length(FID_data(1,:,1))
for j = 1:length(FID_data(1,1,:))
current_data = FID_data(:, j, i);
subaxis(16,16,k, 'Spacing', 0, 'Padding', 0, 'Margin', 0,'SpacingHoriz', 0);
plot(current_data)
ylim([0 a])
k = k+1;
set(gca, 'XTickLabel', []);
set(gca, 'YTickLabel', []);
set(gca, 'Color','w', 'XColor','b', 'YColor','b')
end
end
frame = getframe(gcf);
spectral_data = frame.cdata;
imshow(imageData,'Parent',axes_handle);
numRows = length(FID_data(1,:,1));
numCols = length(FID_data(1,:,1));
% Determine the size of each grid cell
[height, width, ~] = size(spectral_data);
cellHeight = height / numRows;
cellWidth = width / numCols;
% Prompt the user to select a grid section
[x, y] = ginput(1);
% Calculate the selected grid indices
selectedRow = ceil(y / cellHeight);
selectedCol = ceil(x / cellWidth);
% Display the selected grid indices
app.Label.Text = ['X: ', num2str(selectedRow), ', Y: ', num2str(selectedCol)]
However, when I run this and load my data, instead of creating the grid over the figure I display, the app opens a new figure of the subplots outside the app and does not let me select anything at all. Could anyone help or guide me to solve this problem?
Thank you
1 Comment
Mario Malic
on 23 May 2023
Edited: Mario Malic
on 23 May 2023
Hey,
try using the CurrentAxes or CurrentObject property from the uifigure object to get the handle to the selected subplot that you can use to display.
Answers (1)
Divyanshu
on 16 Jun 2023
Hi Marina,
Here are few points to consider for better understanding of the sample app:
- I have created a sample app which has four subplots.
- These subplots I have created using ‘tiledlayout’ function.
- Initially during the start-up of the app, all the suplots are plotted and corresponding axes to each subplot and the plot itself are hidden.
- Now the user can specify the subplot number in the edit field, based on the choice corresponding subplot is made visible.
Here is the code-view of the app with example of two subplots, which can be extended further based on use-case:
methods (Access = private)
% Code that executes after component creation
function createPlot(app)
fig = uifigure;
p = uipanel(fig);
t = tiledlayout(p, 2, 2);
x = [1:5];
y = [1,4,6,9,11];
app.ax1 = nexttile(t);
app.p1 = plot(app.ax1,x,y);
app.ax2 = nexttile(t);
app.p2 = plot(app.ax2,2*x,y);
set(app.ax1,'visible','off');
set(app.p1,'visible','off');
set(app.ax2,'visible','off');
set(app.p2,'visible','off');
end
% Button pushed function: SelectButton
function DisplayTile(app, event)
app.SPN = app.SubPlotEditField.Value;
if(app.SPN == 1)
set(app.ax1,'visible','on');
set(app.p1,'visible','on');
set(app.ax2,'visible','off');
set(app.p2,'visible','off');
elseif(app.SPN == 2)
set(app.ax2,'visible','on');
set(app.p2,'visible','on');
set(app.ax1,'visible','off');
set(app.p1,'visible','off');
end
end
end
Please refer the following documentation for further details on ‘tiledlayout’ function:
0 Comments
See Also
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!