Initialise a variable which changes sizer each iteration in simulink coder

1 次查看(过去 30 天)
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 个评论
Vishal Varadraj
Vishal Varadraj 2022-2-1
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
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.

请先登录,再进行评论。

回答(0 个)

类别

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