simulink - function matlab: array initialization for more entity -


i need simulink function: have n entities, in example 2.

  • attribute entity 1: id = 1 , valore = 5
  • attribute entity 2: id = 2 , valore = 3

i need call function 1 time instead of looping , if exists y = ... don't create anymore.

function y = fcn(valore,id)  y=zeros(1,2);  persistent start;  if isempty(start)     start=zeros(1,2); end  if (id==0)   return  end  y(id) = start(id); start(id) = start(id) + valore; 

enclose values of y

y =      0     0 y =      0    36 start = 60    39 y =      0     0 y =     60     0 start = 65    39 

i would, example

y = 55 33 y = 60 33 y = 65 36 

without call y = zeros (1,2) each function call.

as far know, code generation, first appearance of variable must establish size. first appearance of variable cannot subscripted. need use zeros.

but there not problem! can values of y in different ways: example, 1. easiest way - use y use start - make persistent , let save values between function calls. 2. if need use y value somewhere else can add third variable @ entrance y , save result it.

i use third way:

function y1 = fcn(y, id, valore) y1 = zeros( 1, 2 ); y1 = y; 

and can want.

i sorry if misunderstood question


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -