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
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.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!