MATLAB Answers

Passing variables in GUI's in AppDesigner

130 views (last 30 days)
royed
royed on 9 Feb 2020
Commented: royed on 10 Feb 2020
Hello, so, I want to pass a variable from one GUI to another GUI after I use a PushButton. I have attached a code:
classdef GUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
BlindDeconvolutionPanel matlab.ui.container.Panel
UIAxes matlab.ui.control.UIAxes
UploadButton matlab.ui.control.Button
DeconvoluteButton matlab.ui.control.Button
ParametersPanel matlab.ui.container.Panel
GaussianVarianceEditFieldLabel matlab.ui.control.Label
GaussianVarianceEditField matlab.ui.control.NumericEditField
IterationsEditFieldLabel matlab.ui.control.Label
IterationsEditField matlab.ui.control.NumericEditField
end
properties (Access = public)
image % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Button pushed function: UploadButton
function UploadButtonPushed2(app, event)
[file1,path] = uigetfile({'*.png';'*.jpg';'*.tiff';'*.jpeg'},...
'File Selector');
file=fullfile(path,file1)
a=imread(file)
app.image=imcrop(a,[145 58 370 370])
imshow(app.image,'Parent',app.UIAxes)
title(sprintf('%s',file1),'Parent',app.UIAxes)
end
% Button pushed function: DeconvoluteButton
function DeconvoluteButtonPushed(app, event)
GUI2 % here is the problem
% PSF = fspecial('gaussian',[371 371],app.GaussianVarianceEditField.Value)
% [J,psfr]=deconvblind(app.image,PSF,app.IterationsEditField.Value)
% % psfr=fft(psfr)
% figure
% %J=J./max(max(J))
% subplot(224)
% imagesc(J)
% title('Output Image')
% colorbar
% subplot(223)
% imagesc(psfr./max(max(psfr)))
% title('Extracted PSF')
% colorbar
% subplot(221)
% imagesc(PSF./max(max(PSF)))
% title('Original PSF')
% colorbar
% subplot(222)
% imagesc(app.image)
% title('Original ')
% colorbar
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 548 309];
app.UIFigure.Name = 'UI Figure';
% Create BlindDeconvolutionPanel
app.BlindDeconvolutionPanel = uipanel(app.UIFigure);
app.BlindDeconvolutionPanel.TitlePosition = 'centertop';
app.BlindDeconvolutionPanel.Title = 'Blind Deconvolution';
app.BlindDeconvolutionPanel.FontWeight = 'bold';
app.BlindDeconvolutionPanel.Position = [0 2 549 308];
% Create UIAxes
app.UIAxes = uiaxes(app.BlindDeconvolutionPanel);
title(app.UIAxes, '')
xlabel(app.UIAxes, '')
ylabel(app.UIAxes, '')
app.UIAxes.XColor = 'none';
app.UIAxes.XTick = [];
app.UIAxes.YColor = 'none';
app.UIAxes.YTick = [];
app.UIAxes.ZColor = 'none';
app.UIAxes.ZTick = [];
app.UIAxes.Position = [1 43 280 234];
% Create UploadButton
app.UploadButton = uibutton(app.BlindDeconvolutionPanel, 'push');
app.UploadButton.ButtonPushedFcn = createCallbackFcn(app, @UploadButtonPushed2, true);
app.UploadButton.Position = [362 246 100 22];
app.UploadButton.Text = 'Upload';
% Create DeconvoluteButton
app.DeconvoluteButton = uibutton(app.BlindDeconvolutionPanel, 'push');
app.DeconvoluteButton.ButtonPushedFcn = createCallbackFcn(app, @DeconvoluteButtonPushed, true);
app.DeconvoluteButton.Position = [363 188 100 22];
app.DeconvoluteButton.Text = 'Deconvolute';
% Create ParametersPanel
app.ParametersPanel = uipanel(app.BlindDeconvolutionPanel);
app.ParametersPanel.TitlePosition = 'centertop';
app.ParametersPanel.Title = 'Parameters';
app.ParametersPanel.FontWeight = 'bold';
app.ParametersPanel.Position = [295 19 237 136];
% Create GaussianVarianceEditFieldLabel
app.GaussianVarianceEditFieldLabel = uilabel(app.ParametersPanel);
app.GaussianVarianceEditFieldLabel.HorizontalAlignment = 'right';
app.GaussianVarianceEditFieldLabel.Position = [9 78 106 22];
app.GaussianVarianceEditFieldLabel.Text = 'Gaussian Variance';
% Create GaussianVarianceEditField
app.GaussianVarianceEditField = uieditfield(app.ParametersPanel, 'numeric');
app.GaussianVarianceEditField.Tooltip = {'enter a number possibly 20 or higher'};
app.GaussianVarianceEditField.Position = [130 78 100 22];
% Create IterationsEditFieldLabel
app.IterationsEditFieldLabel = uilabel(app.ParametersPanel);
app.IterationsEditFieldLabel.HorizontalAlignment = 'right';
app.IterationsEditFieldLabel.Position = [46 24 55 22];
app.IterationsEditFieldLabel.Text = 'Iterations';
% Create IterationsEditField
app.IterationsEditField = uieditfield(app.ParametersPanel, 'numeric');
app.IterationsEditField.Tooltip = {'Keep Iterations low 2 works best '};
app.IterationsEditField.Position = [131 24 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = GUI
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
After I press the DeconvoluteButtonPushed2 I want the new GUI to come up and all the variables in this GUI should be passed to the new GUI. Please suggest me something, thanks!

  0 Comments

Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 10 Feb 2020
This document and this example are helpful.
Tips are,
In GUI1, you need to set properties as public (properties (Access = public)) in order to pass the variables to GUI2.
In GUI2, you need to define startupFcn and add an input argument of GUI1 so that GUI1 can call GUI2 with its object.
Defining CallingApp property in GUI2 to store the object of GUI1.
properties (Access = private)
% This is properties of GUI2
CallingApp % GUI1 object
end
Then, define startupFcn in GUI2.
function startupFcn(app, mainapp)
% This is startupFcn of GUI2
% Store GUI1 app object
app.CallingApp = mainapp;
end
Now, you can access variables of GUI1 in GUI2 by using app.CallingApp.
In GUI1, you can call GUI2. Suppose GUI2 name is GUI2.mlapp,
function DeconvoluteButtonPushed(app, event)
% This is button pushed function in GUI1
% Call dialog box with input values
GUI2(app);
end

More Answers (0)


Translated by