Simulink running with inputs like a MatLab function without using workspace
7 次查看(过去 30 天)
显示 更早的评论
I'm making a matlab function that will run and plot and save the relevant data from my simulink models to be used throughout development of my simulink model.
It will be of the form
plotSimData(simName,[inputs],[inputNames],[outputNames])
This function will then load the inputs into Simulink automatically, then plot the output data with the right legend and axes etc.
The simulink model I'm using to test is just a simple one for testing.
It takes two inputs, puts one to the power of the other and integrates it over time. In reality, the only important part is that the inputs are constants that will determine the overall simulation, eg. mass, inertia, etc., and the model will be ran many times with varying mass and inertia to find the optimal result.
I feel like this should be very easy to do, just have the simulink run with the given parameters.
So far, my code is this:
function [~,~] = plotSimData (fName,inputVals)
simIn = Simulink.SimulationInput(fName) ;
inputs = repmat( struct('Val',ones(length(inputVals))) , length(inputVals) , 1 ) ;
for each = 1 : length(inputVals)
inputs(each).Val = inputVals(each) * ones(length(inputVals)) ;
end
simIn = simIn.setExternalInput(inputs.Val) ;
simOut = sim(simIn) ;
hold all
mediary = simOut.yout ;
for each = 1 : numElements(mediary)
out = mediary{each}.Values.Data ;
plot(simOut.tout,out,'DisplayName',string(each))
end
legend ;
end
I understand that the simIn.setExternalInput() takes variables over a time, but they won't change. What's the best way to input constants from MatLab code?
Note, though I'm sure you've noticed, I can't just save to workspace, as since I'm running the MatLab function externally, the variables initialised in this function aren't saved to the workspace, but some sort of other cache or other workspace that isn't accessed by Simulink.
Tl;dr,
How can I input constants to my Simulink model without using the workspace?
0 个评论
回答(1 个)
Suraj
2023-3-28
Hi George,
It is my understanding that you would like to input constants into your Simulink model without having access to the global workspace, as your MATLAB function is being run externally.
For this, you can use the setVariable method on the SimulationInput object, that allows you to create a variable in the global workspace that is then passed onto your model. You can read more about it here.
Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!