How to pass inputs from excel to an inport using just code

Hi,
I want to send inputs from an excel file to an inport of a simulink model. I also want to run it using 'sim' command. Any ideas on this?

 채택된 답변

Harald
Harald 2023년 7월 25일

0 개 추천

Hi Harish,
when you use inports, you will typically set something like [t, u] as inputs, so I suppose what you want to do is define t and u or whatever you call these variables from a spreadsheet? In that case, I would perform steps like importing the spreadsheet and extracting t and u from this in a model callback, see https://www.mathworks.com/help/simulink/ug/model-callbacks.html.
Best wishes,
Harald

댓글 수: 7

Hi, thanks for the reply. I am kind of looking for this.
I have a simple simulink model with input port passed through a gain and an output port. I want to run a series of simulations where the inputs for this port and taken from an excel table. Now, I want to run these series of simulations using Matlab code and without opening Simulink. (Using load_system and sim command)
Hi,
so, it seems that rather than working with fixed input as I understood from the initial question, you want to vary the input? In that case I would recommend using Simulink.SimulationInput as shown here: https://www.mathworks.com/help/simulink/ug/example-of-parallel-simulations-workflow.html
Remarks:
  • In your case, you would use setVariable instead of setBlockParameter
  • If you want to run the simulations sequentially, use sim instead of parsim
  • You can set 'ShowSimulationManager','off','ShowProgress','off' if you don't want to see these.
I should also note that the best assistance can be given if you provide an example model and example data.
Best wishes,
Harald
Hi,
Thankyou for the answer. It helped me partly, but still did not solve the problem. I am attaching the screenshot of what I am looking for.
In the above model, I want to pass the inputs [10 20 30 40] to the input port and collect the outputs to see how my model reacts to different inputs. I want to do everything using code,without touching the model. Could you please guide me further?
Hi Harish,
please attach the model as a file so that I can see how you have configured the data for the Inport.
Also, do you want to pass those inputs for one simulation or separate simulations, so 10 for the first simulation, 20 for the second simulation, etc.?
Additionally, please share any code with Simulink.SimulationInput that you have already written.
Thanks and best wishes,
Harald
Hi Harald,
the following is the code I have written
open_system('model');
setPoints = 10:10:40
spLength = size(setPoints, 2);
for i = 1:spLength
simIn(i) = Simulink.SimulationInput('model')
simIn(i) = setVariable(simIn(i), 'inputPort', setPoints(i))
end
out = sim(simIn)
I want to run them as seperate simulations
Harald
Harald 2023년 7월 28일
편집: Harald 2023년 7월 28일
Hi Harish,
the code looks good to me.
You will need to adjust the model to let the Inport block know that it should be using that inputPort variable. In the Model Explorer, open a configuration and navigate to the Data Import / Export tab and edit Input. Just to give an example:
In real life, you would specify Input as a matrix with (# Input blocks + 1) many columns: first column is time, the other columns each correspond to one Input block.
You can then extract the simulation results from out, e.g. look at
out(1).yout{1}.Values.Data
Best wishes,
Harald
Thankyou for the solution. This helped me and it works now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2023년 7월 25일

댓글:

2023년 7월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by