필터 지우기
필터 지우기

Using App Designer to control inputs to an outside .m file - basic control

조회 수: 15 (최근 30일)
Kyle
Kyle 2023년 6월 13일
편집: Matt J 2023년 6월 14일
TL:DR I was handed off code I want to update into an app designer that uses a GUI as inputs to a follow up function and am not grasping how to have the GUI talk to outside code and am asking for help on a basic example.
Background - I know this question has been answered a few times on here. But i am still lost as the examples are a bit to high level for me. I have been using matlab for almost 10 years now but only in the basic editor. Additionally, it is the only coding language I know.
I was handed some code from a previous individual that hand coded a GUI. Example:
Signal_Title = uicontrol(Settings_LP,'Style','text',...
'String','Signal Analysis Setup',...
'Units','normalized',...
'Position',[GUI_Spacing_W,1-(S1B_Title_H+GUI_Spacing_H),S1B_Title_W*2,S1B_Title_H],...
'Fontsize',Main_Title_Font);
% Saving height
height_1 = 1-(S1B_Title_H+GUI_Spacing_H);
etc etc and it goes on for 1000 lines of code to create:
Each checkbox is an initial condition that will be used in a following funciton.
I wanted to update this do app designer and I have been struggling. I was able to build the figure to look exactly the same but I am struggling with getting the outputs to be controlled by the GUI.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
So I wanted to break this down to a fundamenatals question. I want this check mark to interact with an outside function.
I wrote the worlds simplest code.
function checkboxer(inputArg1)
if inputArg1 == 1;
figure
end
end
The intention was to be when the GUI closes. if the box is checked. just poplulate an emply figure. and if the box is not checked. do nothing. I have been trying to get this moving for almost 3 days but I struggle with undestanding classes, properties, callbacks etc and none of the YT videos or forum posts have helped me brigde the gap.
classdef checkboxtester < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Check1CheckBox matlab.ui.control.CheckBox
end
properties (Access = public)
Check1 = 1; % Description
end
methods (Access = public)
function appdesignfunction(app)
checkboxer(app.Check1)
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Check1CheckBox
function Check1CheckBoxValueChanged(app, event)
value = app.Check1CheckBox.Value;
if value == 1
app.Check1 = 1;
end
end
end
This is the current appdesigner code. Following along a few of the posts I made the outer function a function in app designer, and then to be honest I dont have a clue how properties work or how to call the section of the value. I have been struggling on this basic case but I feel like if I get it explanded in basic terms I should be able to brigde the gap.
  댓글 수: 2
Rik
Rik 2023년 6월 13일

It clicked for me once I understood that AppDesigner results in a class. Callbacks actually call methods (or embedded functions if you want to make things more difficult), and you can store values you need in private properties. In GUIDE you write a big function. Every callback calls in internal function and you can store values in fields of a struct you save and restore with guidata.

Exporting data once a GUI closes can be a bit tricky, but it can be done with both styles.

Did you come across this thread?

Kyle
Kyle 2023년 6월 13일
This thread is the one that I was trying to use as it seems like a very straigthforward example. But even your first sentence has me... So what that app desigener results in a class? Why is that important.
Further, so in its currents state. it feels like to me this should be doing what i want. In the basic example on the bottom half, what is preventing that figure from populating when I click the check mark?

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2023년 6월 14일
편집: Matt J 2023년 6월 14일
From your description, it sounds like the app code ought to be much simpler:
classdef checkboxtester < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Check1CheckBox matlab.ui.control.CheckBox
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Check1CheckBox
function Check1CheckBoxValueChanged(app, event)
checkboxer( app.Check1CheckBox.Value );
end
end
...
end
In particular, it seems redundant to have an additional property that stores the state of the check box. It is already stored in app.Check1CheckBox.Value. However, you might wish to add a property that stores a handle to the figure created by checkboxer():
function varargout=checkboxer(inputArg1)
varargout={};
if inputArg1 == 1;
varargout{1}=figure;
end
end
and then,
classdef checkboxtester < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Check1CheckBox matlab.ui.control.CheckBox
end
properties (Access = public)
figHandle; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Check1CheckBox
function Check1CheckBoxValueChanged(app, event)
app.figHandle = checkboxer( app.Check1CheckBox.Value );
end
end
...
end
  댓글 수: 2
Kyle
Kyle 2023년 6월 14일
편집: Kyle 2023년 6월 14일
Can you provide a bit more information for me? So what do you mean stores a handle? I dont particularly understand why it is redunant as I dont understand why I make properties in the first place.
Further, why does the code fail without the output command of varargout in the checkboxer.m file? What is that doing that allows the app designer to run?
Finally, If I wanted to store the state of that selection and only have the figure populate once the GUI closes, (you dont need to code this if you dont want to) what changes here?
Matt J
Matt J 2023년 6월 14일
편집: Matt J 2023년 6월 14일
Can you provide a bit more information for me? So what do you mean stores a handle? I dont particularly understand why it is redunant as I dont understand why I make properties in the first place.
Well, properties are a way of storing data in the app object that might be needed later as the GUI runs. The on/off state of the checkbox is indeed data that your callback functions may want to access later during the lifetime of the GUI window. My point though, was that it is unnecessary to store a second copy of it in app.Check1, since it is already available as a property of the component app.Check1CheckBox. Also, having two different copies of this data floating around within your app is error-prone, since you then have to worry about keeping them consistent.
If you wanted to have app.Check1 be shorter alias for app.Check1CheckBox.Value, there are ways to do that by making Check1 a Dependent property,
properties (Access = public, Dependent)
Check1 = 1; % Description
end
but that might be discussion for another time.
Further, why does the code fail without the output command of varargout in the checkboxer.m file? What is that doing that allows the app designer to run?
In your posted code, checkboxer doesn't return any output arguements. Therefore, it does not allow you to save a handle to the figure that it creates. My modification gives it that capability.
Finally, If I wanted to store the state of that selection and only have the figure populate once the GUI closes, (you dont need to code this if you dont want to) what changes here?
Ordinarily you would write a delete() method for the handle to the GUI that does the work you describe. In the specific case of app designer handles, unfortunately, the delete() method is created automatically by appdesigner and is not editable. However, you could make a "Populate and Close" pushbutton that will populate the figure and then close the app.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by