How do I link to Simulink variable from app designer

23 次查看(过去 30 天)

Ive created a model in app designer and want to change an image in the app based on the state of a value in the model. For example image is a pump and if the Simulink model state is 0 I want to use the closed pump image, if the state is 1 use the pump running image etc. The image widget doesn’t support bindings. that’s OK I can set up my own function and schedule it to run every second to check the pump state. In the model I have a variable setup that’s being logged. In the app I’m then trying to access that variable using: app.Simulation.LoggedSignals(‘PumpCtrlMdl/PumpState:1’).Values But this returns empty. How would I update my code so I get the value of the output value correctly?

回答(1 个)

Jack
Jack 2025-4-4
You can access the latest value by retrieving the logged signal from the SimulationOutput object’s logsout dataset. Instead of indexing directly with parentheses, try using the getElement method. For example:
pumpSignal = app.Simulation.LoggedSignals.getElement('PumpCtrlMdl/PumpState:1');
currentPumpState = pumpSignal.Values.Data(end);
This will return the most recent data point from the logged timeseries. Make sure that the signal is correctly logged in your model and that its name exactly matches the one used in getElement. Also, ensure that your simulation has run long enough to accumulate data.
Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.
  3 个评论
Jack
Jack 2025-4-4
It sounds like your simulation output might be using the "Logsout" property instead of "LoggedSignals." In most cases, when you run a Simulink simulation with signal logging enabled, the SimulationOutput object (e.g., app.Simulation) has a property called Logsout that is a Dataset. You can then use its getElement method. For example:
pumpSignal = app.Simulation.Logsout.getElement('PumpCtrlMdl/PumpState:1');
currentPumpState = pumpSignal.Values.Data(end);
Make sure that your model is configured to log the signal (using the Logsout option) and that the signal name exactly matches what you’re using here. If you still get errors, double-check your simulation settings to ensure that signal logging is set up to produce a Dataset rather than a simsignals object.
Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.
Neil Smithson
Neil Smithson 2025-4-7
Thank you for all your help. Unfortunately I didn't have a Logsout option, however I did try lots of different apporaches and finally I found a solution to this. Adding here in case it helps anyone else in the future:
I set up a variable in my app class - pumpState. Then in startupFcn I was able to bind the logged signal to this class variable:
bind(app.Simulation.LoggedSignals, 'PumpCtrlModel/PumpState:1'], app, 'pumpState');
This worked by linking the two together. Finally I set up a periodic timer to call a function which checks the state and updates the image if the state has changed.

请先登录,再进行评论。

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by