Using a image variable from one GUI function to another in AppDesigner

18 views (last 30 days)
royed
royed on 6 Feb 2020
Commented: J. Alex Lee on 6 Feb 2020
Hello,
I am trying to build a GUI interface for blind deonvolution for some fluroscence images. I have an issue with assigning the variables from one ButtonPushed function to another ButtonPushed fuction. I tried to make the image variable Global but assigning it startupFcn but it didn't work. The issue is I want the image variable b from UploadButtonPushed2 to DeconvoluteButtonPushed
classdef GUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
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
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
global b
end
% Button pushed function: UploadButton
function UploadButtonPushed2(app, event)
[file,path] = uigetfile({'*.png';'*.jpg';'*.tiff';'*.jpeg'},...
'File Selector');
a=imread(file)
b=imcrop(a,[145 58 370 370])
imshow(b,'Parent',app.UIAxes)
title(sprintf('%s',file),'Parent',app.UIAxes)
end
% Button pushed function: DeconvoluteButton
function DeconvoluteButtonPushed(app, event)
load(b) % there is the issue I want the image variable b from UploadButtonPushed2 to DeconvoluteButtonPushed
PSF = fspecial('gaussian',[371 371],app.GaussianVarianceEditField.Value)
[J,psfr]=deconvblind(b,PSF,app.IterationsEditField.Value)
figure
subplot(131)
imagesc(J)
colorbar
subplot(132)
imagesc(psfr)
colorbar
subplot(133)
imagesc(PSF)
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 566 322];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
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 = [12 53 280 234];
% Create UploadButton
app.UploadButton = uibutton(app.UIFigure, 'push');
app.UploadButton.ButtonPushedFcn = createCallbackFcn(app, @UploadButtonPushed2, true);
app.UploadButton.Position = [373 256 100 22];
app.UploadButton.Text = 'Upload';
% Create DeconvoluteButton
app.DeconvoluteButton = uibutton(app.UIFigure, 'push');
app.DeconvoluteButton.ButtonPushedFcn = createCallbackFcn(app, @DeconvoluteButtonPushed, true);
app.DeconvoluteButton.Position = [374 198 100 22];
app.DeconvoluteButton.Text = 'Deconvolute';
% Create ParametersPanel
app.ParametersPanel = uipanel(app.UIFigure);
app.ParametersPanel.TitlePosition = 'centertop';
app.ParametersPanel.Title = 'Parameters';
app.ParametersPanel.FontWeight = 'bold';
app.ParametersPanel.Position = [306 29 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.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.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
Please suggest me a possible solution to this problem

Accepted Answer

J. Alex Lee
J. Alex Lee on 6 Feb 2020
Create a property for your app class to hold the image.
You appear to be using appdsigner. In appdesigner's code view, on the left panel look for "properties" and add a new one, call it Image (or b, but probably Image is better).
Remove your global declaration.
Your upload callback should look like
function UploadButtonPushed2(app, event)
[file,path] = uigetfile({'*.png';'*.jpg';'*.tiff';'*.jpeg'},...
'File Selector');
a=imread(file)
app.Image=imcrop(a,[145 58 370 370]) %%%%%%%%%%% this line edited
imshow(app.Image,'Parent',app.UIAxes) %%%%%%%%%%% this line edited
title(sprintf('%s',file),'Parent',app.UIAxes)
end
and your deconv button callback should look like
function DeconvoluteButtonPushed(app, event)
% load(b) % there is the issue I want the image variable b from UploadButtonPushed2 to DeconvoluteButtonPushed
PSF = fspecial('gaussian',[371 371],app.GaussianVarianceEditField.Value)
[J,psfr]=deconvblind(app.Image,PSF,app.IterationsEditField.Value) %%%%% this line edited
figure
subplot(131)
imagesc(J)
colorbar
subplot(132)
imagesc(psfr)
colorbar
subplot(133)
imagesc(PSF)
colorbar
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!