필터 지우기
필터 지우기

Simulink running with inputs like a MatLab function without using workspace

조회 수: 5 (최근 30일)
George Smith
George Smith 2023년 2월 13일
답변: Suraj 2023년 3월 28일
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?

답변 (1개)

Suraj
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.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by