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

조회 수: 199 (최근 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월 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 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