Matlab App Designer Update GUI on Object PostSet Event
Ältere Kommentare anzeigen
I am creating a simple GUI to interact with an arduino board over serial. The user will be able to enter desired parameters from the UI, which are stored in a "command state" object, then transmit those parameters on a button push. On the flip-side, health & status updates will come from the arduino periodically and update another "reported state" object, which in turn will update various gauges in the GUI. This latter step is what I'm having trouble with.
In short:
How can GUI elements listen for a change to an object property and update accordingly?
What I have so far:
The object in question is declared as a handle, with "SetObservable" enabled on its properties so the "PostSet" event should be active. For this example, I'll add a single property, the vector P and initialize it as all zeros. For class methods we have a simple constructor and a setter for P.
classdef DroneState < handle
properties (AbortSet, SetObservable)
P = [0, 0, 0, 0]
end
methods
function obj = DroneState(P_in)
% Constructor
if nargin ~= 0
obj.P = P_in;
end
end
function set.P(obj, P_in)
%Set P Values. Accepts a 1x4 array of new values. To leave a
% value unchanged, submit NaN in it's index. Valid Range 0:100.
% Catch bad input array
if length(P_in) ~= 4
disp(P_in);
error("Invalid P Array.\nValid format: [ P1 P2 P3 P4 ]");
end
% Iterate P values, set valid inputs, leave NaN indices.
nan_bool = isnan(P_in);
for i = 1:4
if (P_in(i) >= 0) && (P_in(i) <= 100)
obj.P(i) = P_in(i);
elseif nan_bool(i)
continue
else
error("Invalid P Value: %d\nAcceptable range: 0:100 (%%)",P_in(i));
end
end
end
end
end
On the GUI end, I define the two state objects as public properties of the app, and declare them in the startupFcn. I also have a testing button that sets the values of deployed.P
classdef test_stand_control < matlab.apps.AppBase
...
properties (Access = public)
deployed DroneState % Describes the current state of the drone, as recieved from teensy serial
commands DroneState % Describes the command state of the drone, to be deployed to teensy serial
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, port, baud)
% Instatiate DroneState objects (empty)
app.deployed = DroneState;
app.commands = DroneState;
end
% Button pushed function: TestButton
function TestButtonPushed(app, event)
app.deployed.P = [10 20 30 40];
end
end
end
Finally, I have another private function within the app that (should) update the values of four gauges when the P PostSet event is caught:
methods (Access = private)
function deployedP(app,src,event)
% Update P Gauges on display in the event that updates are made
% to the deployed DroneState values.
disp("deployedPSetCallback Triggered.")
app.P1Gauge.Value = app.deployed.P(1);
app.P2Gauge.Value = app.deployed.P(2);
app.P3Gauge.Value = app.deployed.P(3);
app.P4Gauge.Value = app.deployed.P(4);
end
end
I understand logically that the missing component is a listener waiting for the P PostSet event, but I cannot figure out how to deploy this despite many attempts. Including:
P_listener = addListener(app.deployed,'P','PostSet',...
@(src, event) deployedP(app,src,event));
in the startupFcn throws the error:

I know I can't be the only one trying to solve this problem, please help!
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Develop Apps Using App Designer finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!