How to execute GUI callbacks in separate .m files.

11 visualizaciones (últimos 30 días)
Alena Schwartz
Alena Schwartz el 7 de Oct. de 2021
Respondida: Animesh el 21 de Feb. de 2024
I have written a GUI using uifigure which contains alot of callbacks. As a result, it is becoming challenging to keep the file organzied. I would like to have a main file which initializes the GUI and adds the elements to it. I would also like to have separate .m file for each of the callbacks (ie: listbox, push button, slider).
Based on what I have read online, I have gathered that I will need a main file, a file to call all of the individual callbacks, and files for each of the callbacks. Below is part of my code to help show what my attempt at doing this looks like.
I am unsure of how to fix my code to get it to work properly with the callbacks in indiviudal files.
% main file, set up gui
slicingObjects = uifigure;
slicingObjects.Color = [0.8 0.8 1];
slicingObjects.Position = [45 70 1410 900];
slicingObjects.Units = 'Pixels';
slicingObjects.Resize = 'Off';
slicingObjects.Visible = 'on';
slicingObjects.Name = 'Slicing Objects';
remotePanel = uipanel(slicingObjects,'units','pixels');
RemotePanel.WordWrap = 'on';
remotePanel.Title = 'Enter phyphox Remote Acess URL';
remotePanel.TitlePosition = 'centertop';
remotePanel.BackgroundColor = 'white';
remotePanel.FontName = 'Century Gothic';
remotePanel.FontSize = 20;
remotePanel.FontWeight = 'bold';
remotePanel.Position = [900 325 500 175];
remoteText = uieditfield(remotePanel);
remoteText.Value = 'EX: http://104.39.161.87/';
remoteText.BackgroundColor = 'white';
remoteText.FontName = 'Century Gothic';
remoteText.FontSize = 16;
remoteText.Enable = 'off';
remoteText.Position = [20 80 460 45];
remoteText.HorizontalAlignment = 'center';
remoteText.ValueChangedFcn = {@callbackRemoteText};
remoteURL = uieditfield(remotePanel,'text');
remoteURL.Value = 'Enter URL: ';
remoteURL.BackgroundColor = 'white';
remoteURL.FontName = 'Century Gothic';
remoteURL.FontSize = 16;
remoteURL.Enable = 'on';
remoteURL.Position = [20 20 460 45];
remoteURL.HorizontalAlignment = 'center';
remoteURL.ValueChangedFcn = {@callbackRemoteURL};
% calling callbacks
function callbacks = GUIcallbacks
callbacks = struct('remoteText',@callbackRemoteText,'remoteURL',@callbackRemoteURL);
function callbackRemoteURL_call(hObject,~,handles)
callbackRemoteURL(hObject,~,handles)
end
end
% remote URL callback
function callbackRemoteURL(hObject,~,slicingObjects)
handles = getappdata(hObject,'hObject')
remoteURL = remoteURL.Value;
% MORE CODE HERE
end
When I run the code, I get the following error:
handles =
[]
Unrecognized function or variable 'remoteURL'.
Error in callbackRemoteURL (line 10)
remoteURL = remoteURL.Value;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 470)
Error while evaluating EditField PrivateValueChangedFcn.

Respuestas (1)

Animesh
Animesh el 21 de Feb. de 2024
It seems like you are trying to structure your MATLAB GUI code by separating the callbacks into different files.
The error encountered is because the remoteURL variable is not defined in the scope of the callbackRemoteURL function. To fix this, you should pass the remoteURL object to the callback function as an argument.
Here is one of the ways in which you can organize your code:
Main file (main.m): It will contain the GUI setup. It will also initialize the callback functions and assign them to appropriate UI components.
% Setup GUI
slicingObjects = uifigure;
% ... (rest of your setup code)
% Set up callbacks
callbacks = GUIcallbacks;
% Set the callback functions
remoteText.ValueChangedFcn = {@callbacks.remoteText, slicingObjects};
remoteURL.ValueChangedFcn = {@callbacks.remoteURL, slicingObjects};
Callbacks file: It contains all callback functions.
% GUIcallbacks
function callbacks = GUIcallbacks
callbacks = struct(...
'remoteText', @callbackRemoteText, ...
'remoteURL', @callbackRemoteURL ...
);
end
Individual callback files:
% callbackRemoteURL.m
function callbackRemoteURL(hObject, event, slicingObjects)
% hObject refers to the UI component that triggered the callback
% slicingObjects is the main figure which you passed as an argument
remoteURL = hObject.Value;
End
% callbackRemoteText.m
function callbackRemoteText(hObject, event, slicingObjects)
% CODE HERE
End
Make sure that each callback function file is in the same directory as the “main.m file or in MATLAB's path, so they can be accessed when running the main.m file. Also, ensure that the arguments passed to the callback functions match the signature of the function definitions.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by