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?

 Accepted Answer

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 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 25 Jul 2023

Commented:

on 28 Jul 2023

Community Treasure Hunt

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

Start Hunting!