Matlab App Custom Component Not Behaving as Expected
8 views (last 30 days)
Show older comments
Hi everyone,
First attempt at making my own custom component.
It is a 4 way button lineup, with 3 options + blank as the 4th.
I got5 the base functionality down, so that the buttons can be toggled on or off and only 1 can be selectd at one time. I also go it to output this info to a public varibale to be sued by another app.
What I can't get to work is the reverse communcation. As in: from the main app, I want to SET the current button that is pushed and force the component to update as such. Im aware there is an "update" area in the callbacks that is code that is supposed to run when the any part of the component is interracted with, but |I am ending up in a recursive loop.
An example of the functioning is:
- VC button is pressed
- Program needs to determine if the button was on or off before
- Check the status variable
- Status is [0 0 0], therefore the VC button was unselected.
- Select it
- End.
From the main app, I would like to then set the status to [1 0 0] and see the VC button being selected as if the user had interracted with it, like a normal matlab toggle UI would work.
The code is included below and the component is attached.
Personally, I think it is a gap of knowledge on my side rgarding implementaion of callbacks and the use of the Notify function, as stated in matlab documentation, but I really can figure it out. I think it would be helpful if said documentation, regarding the communication between a main app and component, would actually state WHERE these parts of the code should be placed rather than vaguely mentioneing "write this code".
% Events with associated public callbacks
events (HasCallbackProperty, NotifyAccess = private)
OpTypeSet
end
properties (Access = private)
OpType string {mustBeText(OpType)}
end
properties (Access = public)
Status (1,3) double = [0,0,0];
end
methods (Access = private)
function ResetComp(comp)
comp.vc_OFF.Visible = "on"; comp.vc_OFF.Enable = "on";
comp.rpm_OFF.Visible = "on"; comp.rpm_OFF.Enable = "on";
comp.od_OFF.Visible = "on"; comp.od_OFF.Enable = "on";
comp.vc_ON.Visible = "off"; comp.vc_ON.Enable = "off";
comp.rpm_ON.Visible = "off"; comp.rpm_ON.Enable = "off";
comp.od_ON.Visible = "off"; comp.od_ON.Enable = "off";
end
function SetComp(comp, event)
status = comp.Status;
try
event = event.Source.Tag;
catch
end
ResetComp(comp)
switch event
case "VC"
if status(1) == 1 % VC currently on
% comp.vc_OFF.Visible = "on"; comp.vc_ON.Visible = "off"; comp.vc_OFF.Enable = "on"; comp.vc_ON.Enable = "off";
comp.OpType = "";
comp.Status = [0 0 0];
elseif status(1) == 0 % VC currently off
comp.vc_ON.Visible = "on"; comp.vc_ON.Enable = "on";
comp.OpType = "VC";
comp.Status = [1 0 0];
end
case "RPM"
if status(2) == 1 % RPM currently on
% comp.rpm_OFF.Visible = "on"; comp.rpm_ON.Visible = "off"; comp.rpm_OFF.Enable = "on"; comp.rpm_ON.Enable = "off";
comp.OpType = "";
comp.Status = [0 0 0];
elseif status(2) == 0 % RPM currently off
comp.rpm_ON.Visible = "on"; comp.rpm_ON.Enable = "on";
comp.OpType = "RPM";
comp.Status = [0 1 0];
end
case "OD"
if status(3) == 1 % OD currently on
% comp.od_OFF.Visible = "on"; comp.od_ON.Visible = "off"; comp.od_OFF.Enable = "on"; comp.od_ON.Enable = "off";
comp.OpType = "";
comp.Status = [0 0 0];
elseif status(2) == 0 % OD currently off
comp.od_ON.Visible = "on"; comp.od_ON.Enable = "on";
comp.OpType = "OD";
comp.Status = [0 0 1];
end
case ""
comp.vc_OFF.Visible = "on"; comp.vc_ON.Visible = "off"; comp.vc_OFF.Enable = "on"; comp.vc_ON.Enable = "off";
comp.vc_OFF.Visible = "on"; comp.vc_ON.Visible = "off"; comp.vc_OFF.Enable = "on"; comp.vc_ON.Enable = "off";
comp.od_OFF.Visible = "on"; comp.od_ON.Visible = "off"; comp.od_OFF.Enable = "on"; comp.od_ON.Enable = "off";
comp.OpType = "";
comp.Status = [0 0 0];
end
end
notify(comp, 'OpTypeSet');
end
% Callbacks that handle component events
methods (Access = private)
% Image clicked function: vc_ON
function vc_ONImageClicked(comp, event)
SetComp(comp, event)
end
% Image clicked function: rpm_ON
function rpm_ONImageClicked(comp, event)
SetComp(comp, event)
end
% Image clicked function: od_ON
function od_ONImageClicked(comp, event)
SetComp(comp, event)
end
% Image clicked function: vc_OFF
function vc_OFFImageClicked(comp, event)
SetComp(comp, event)
end
% Image clicked function: rpm_OFF
function rpm_OFFImageClicked(comp, event)
SetComp(comp, event)
end
% Image clicked function: od_OFF
function od_OFFImageClicked(comp, event)
SetComp(comp, event)
end
end
methods (Access = protected)
% Code that executes when the value of a public property is changed
function update(comp)
SetComp(comp,comp.OpType);
end
3 Comments
chrisw23
on 24 Oct 2023
Edited: chrisw23
on 24 Oct 2023
Without goin too deep...
Instanciating your component returns a figure object with full access to all user control app uicontrols, while giving the callers (main app) reference as an argument to your custom control app, gives full access to the main app uicontrols from the custom control.
Hope it helps
Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!