Create a selectable grid in a MATLAB app

11 次查看(过去 30 天)
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 个评论
Mario Malic
Mario Malic 2023-5-23
编辑:Mario Malic 2023-5-23
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.

请先登录,再进行评论。

回答(1 个)

Divyanshu
Divyanshu 2023-6-16
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:

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by