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
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!