Initialise a variable which changes sizer each iteration in simulink coder

1 view (last 30 days)
How would i go about initialising a variable which changes size each iteration. I've tried three things to get past this
  • The first being and i got that error which does make sense
powerout = zeros(1,1);
Attempted to access index 2 of an array with smaller dimension sizes. The valid index range is 1 to 1. This error will stop the simulation. In MATLAB Function 'ThermalBatterySimulinkModel/MATLAB Function': powerout(n) = SensiblePower;
  • The second being this script of code for which i got this error
powerout = zeros(60,1);
Size mismatch (size [60 x 1] ~= size [1 x 1]). The size to the left is the size of the left-hand side of the assignment.
  • The third being the following which i was not sure how this would work
powerout = zeros(1,1);
coder.varsize('powerout',[1 60]);
'powerout' is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'powerout' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
Any idea how i can initialise a variable/get this code working with this variable. I know that I'm running the loop for 60 iterations and I'm storing 60 values and need to output the final value. I have attached a picture by what I mean.
  3 Comments
Vishal Varadraj
Vishal Varadraj on 1 Feb 2022
I think powerout is defined inside the simulink model, though i must say that I'm completely new to simulink. I've written my script in MATLAB and trying my best to translate that into a simulink Function so im not sure if thats fully correct or not. Any guidance on how i can find that would be helpful. Below is the screenshot of my code ive put into my function block. I need all 60values of powerout recorded as I will index them later on in the code so rewritting on top of previous isn't an option. I've attached a screenshot of my code and marked it in red by what I mean. Thanks!
Benjamin Thompson
Benjamin Thompson on 3 Feb 2022
You do not need to size variables to match the time duration of a simulation. Your S-Function just needs to update the value of all outputs to Simulink at each iteration. Then, connect the output to a "To Workspace" block or use signal logging if you want to save the past values.

Sign in to comment.

Answers (0)

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!