Run simulink simulation in deployed app with from workspace block
13 views (last 30 days)
Show older comments
Hi,
I am running Matlab 2023b. Now let me explain my situation:
I have an app that reads data with an external DLL. That works fine and the signals are stored in a variable called data.
The data variable is a structure with time and looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1740966/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1740971/image.png)
In the model I use from workspace blocks like that:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1740976/image.png)
After reading the data, I assign from the app to the base workspace so the simulink model can access it.
assignin('base','data',app.data);
assignin('base','StopTime',StopTime);
Afterwards I can run the simulation and provide the output to the app.
app.out = sim('LMDhDataCheck_Model');
All of that works fine. Now to my problem. When I compile the app as standalone it does not read the input correctly.
I have tried already to precompile the model and then starting the exe, which does not work. It seems like the input is 0 all the time.
if isdeployed
% Code that gets run only by compiled applications
disp('Run deployed simulink sim');
!LMDhDataCheck_Model;
load('LMDhDataCheck_Model.mat');
vars = who();
TF = contains(vars, 'rt_');
varsFiltered = vars(TF);
for i = 1:length(varsFiltered)
app.out.(eraseBetween((varsFiltered{i,1}),1,3)) = eval((varsFiltered{i,1}));
end
else
% Code that gets run only in development environment
disp('Run development simulink sim');
app.out = sim('LMDhDataCheck_Model');
end
Then I tried to run the following code in the workspace before putting it in the app, but I get an error:
in = Simulink.SimulationInput('LMDhDataCheck_Model')
in = simulink.compiler.configureForDeployment(in)
in = setVariable(in, 'data', data)
in = setVariable(in, 'StopTime', StopTime)
out = sim(in)
The tuned value of the variable 'data' (in the workspace 'global-workspace') has a different type than the variable's default value.
Any help will be appriciated as I cannot find an answer in the forums.
0 Comments
Answers (1)
Kautuk Raj
on 16 Aug 2024
I can observe that you are having issues running a Simulink simulation in a deployed app using the "From Workspace" block and are encountering an error related to a type mismatch for the variable 'data' in the global workspace.
You can inspect the following points to troubleshoot this error:
Type Mismatch Error: The error being encountered is due to the variable 'data' having a different type in the workspace compared to its default value. Cell arrays must have the same size and type for runtime tuning. If they differ, the error message is triggered, and tuning does not take place, which is the desired behaviour.
Handling String Inputs: Arguments passed to the standalone application are by default treated as a char vector. For example, if '1' is passed as an argument, it gets treated as a string '1'. To handle this in deployment, one can add code to convert the string input to the appropriate type. Here is an example:
if ischar(variantControl)
variantControl = str2double(variantControl);
end
This approach ensures that the variable is correctly interpreted in the standalone application.
Ensure that the variable types and sizes match between the development environment and the deployed application.
If cell arrays are being used, consider restructuring your data or using different data types that are supported for runtime tuning.
For handling inputs in a standalone application, ensure proper type conversion as shown in the example above.
By following these guidelines, you should be able to resolve the type mismatch issue and ensure that the Simulink model runs correctly in a deployed application.
0 Comments
See Also
Categories
Find more on Simulink Coder 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!