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

195 ビュー (過去 30 日間)
Paul Richardson
Paul Richardson 2020 年 7 月 3 日
コメント済み: Francesco Bernardini 2022 年 11 月 17 日
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 月 3 日
Thank you, i have attached the mlapp file.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by