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
2023년 7월 25일
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
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에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
