Use a external function in app designer and display the result in a gauge or Edit Numeric Field using live data as inputs of the function
7 views (last 30 days)
Show older comments
Hi,
I am trying to use an external function (please, see 'subcooling.m' file) that I created which can provide me the subcooling of a P-H diagram in my model. This function needs two inputs: p2 (pressure in sensor 2) and t3 (temperature in sensor 3) .I tested the code in matlab setting predefines values for p2 and t3 and it worked perfect. But now, I want to use such function in the dashboard that I created in app designer using live data for p2 and t3 provided by my simulink model during the simulation, in order to display such value of the subcooling in an Edit Numeric field or a Gauge. The values of p2 and t3 are displayed in another Edit Numeric Field in the dashboard, for that reason I define p2 and t3 in the code as "app.nameofthenumericfield.Value" (please see the image with the part of the app designer code used to calculate and display the subcooling).
However, when I call the function in app deginer and then run the simulink model, it displays me a complete unexpected value and it does not change during the simulation. So I do not know if I am calling incorrectly the function in my app designer code, or there is something wrong with my function (which is working perfect when I test it in matlab) or there is another thing that I am not taking into account.
I will really appreciate your comments!!!
Thanks in advance,
0 Comments
Answers (1)
Vinayak
on 24 May 2024
Hi Mauricio,
The image you shared, shows the function being called correctly. Ensure the function is accessible by the app by being in the MATLAB path during runtime.
Since the value is not updating, I recommend polling the model to update the value using a timer. Initialize the timer in the ONButtonPushed function:
function ONButtonPushed(app, event)
set_param('Cooling_System_controlpanel', 'SimulationCommand', 'start');
app.UpdateTimer = timer('ExecutionMode', 'fixedRate', ... 'Period', 1, ... % 1 second period, adjust as needed 'TimerFcn', @(~,~) updateValues(app));
start(app.UpdateTimer);
end
In the update values function:
% Get current values from the Edit Numeric Fields
p2 = app.CompresCondenPressureMPaEditField.Value;
t3 = app.CondenExpanValveTKEditField.Value;
% Alternatively, get values from model outports
% p2 = get_param(''Cooling_System_controlpanel /simout_p2', 'RuntimeObject').OutputPort(1).Data;
% t3 = get_param(''Cooling_System_controlpanel /simout_t3', 'RuntimeObject') .OutputPort(1).Data;
% Calculate subcooling
subcoolingValue = subcooling(p2, t3);
% Update the Subcooling Edit Field
app.SubcoolingEditField.Value = subcoolingValue;
This setup ensures that the values are periodically updated, and the calculations are visible in real-time.
0 Comments
See Also
Categories
Find more on Develop Apps Using App Designer 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!