Update an App Designer listbox if a Simulink model entity is selected

21 次查看(过去 30 天)
[ Seeking help specifically for this in Matlab 2019b ]
I made an App Designer app that populates a listbox with all the currently open Simulink models. My use-case is:
If a user clicks on one of the open models (or an entity in it like a block or signal), I'd like the listbox Value to switch to the selected model. I'm guessing I'd have to create a listener function.
Below, the first image is the model window and the second is the listbox from my Appdesigner app.
So, if the user selects the signal in the first image (blue) from the BCM_LIB model, I'd like the listbox selection (2nd image) to change to BCM_LIB. Similarly, if this user selected something in the SCCM_LIB window, I'd like the listbox selection to change to SCCM_LIB.
TIA
Joe

回答(1 个)

Sreeram
Sreeram 2024-9-27,11:49
Hi Joe,
I could not find a callback which is triggered when the currently selected Simulink model is changed. You can get the currently selected Simulink model using the “gcs” command. Please refer to the documentation below:
I recommend using a timer to periodically check the value of gcs and update the list box accordingly.
Here is how this can be done:
1. Create a private propertyUpdateTimerin the app.
2. In the startup function of the app, create and start a timer. The period of update may be adjusted appropriately.
function startupFcn(app)
app.UpdateTimer = timer(...
'ExecutionMode', 'fixedRate', ...
'Period', 1.0, ...
'TimerFcn', @(~,~) updateListBox(app));
start(app.UpdateTimer);
end
3. Add the timer callback function, ‘updateListBox’ as a private function.
function updateListBox(app)
try
currentModel = gcs; % Get the current Simulink model
if ~strcmp(app.ListBox.Value, currentModel)
app.ListBox.Value = currentModel; % Update the ListBox if there's a change
end
catch
% Handle the case where gcs might fail (e.g., no model open)
app.ListBox.Value = '';
end
end
4. In the app’s close request function, stop and delete the timer object created in step 1 before deletion of ‘app’ object.
function UIFigureCloseRequest(app, event)
stop(app.UpdateTimer); % Stop the timer
delete(app.UpdateTimer);
delete(app)
end
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by