App Designer basics: How can I call a function from within a callback

194 次查看(过去 30 天)
I am trying to find a really simple guide as I am 100% new to creating GUIs in MATLAB. This must be in the documentation but I don't know what to look for or where to start, so apologies.
I have written some functions in MATLAB which I would like to adapt for use in a GUI, but for various reasons I would like to keep the functions within the GUI code rather than call them from an external file (unless this is the only way to do it!). The functions themselves are not important as it's the basics I am getting stuck on.
I would like to be able to call a function from within a callback - i.e. I press a button, a function is called, and then the results of that function are returned to the callback. I created the simple GUI and the callback, and I added a simple function, all through the "code browser".
classdef graphs1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
FilenameEditFieldLabel matlab.ui.control.Label
FilenameEditField matlab.ui.control.EditField
ReadButton matlab.ui.control.Button
ThisfilecontainsTextAreaLabel matlab.ui.control.Label
ThisfilecontainsTextArea matlab.ui.control.TextArea
end
methods (Access = private)
function[test]=square_this(number)
test=number * number;
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ReadButton
function ReadButtonPushed(app, event)
test_value=square_this(12)
app.ThisfilecontainsTextArea.Value = num2str(test_value);
end
end
When I run the GUI I get the error message "Undefined function 'square_this' for input arguments of type 'double'.".
To me it is as if the callback cannot see the function at all - so I assume there must be a basic step I am missing.
I learn best through examples, so I have tried searching for "calling functions from within a callback" but I am having trouble finding the help I need.
Thank you.
  4 个评论
Rik
Rik 2020-7-3
Can you attach the code using the paperclip icon? I don't currently have any ideas about why this error would occur.

请先登录,再进行评论。

采纳的回答

Paul Richardson
Paul Richardson 2020-7-9
OK so maybe bad form to answer one's own question but after doing some digging and finding an example elsewhere, it turns out that all it needs is to also pass the "app" object into the function (even though it is not explicitly used by the function). The code below now works in that mlapp file:
methods (Access = private)
function[test]=square_this(app,number)
test = number * number;
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ReadButton
function ReadButtonPushed(app, event)
test_value=square_this(app,12)
app.ThisfilecontainsTextArea.Value = num2str(test_value);
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by