0

I'm using a Matlab level 1 S-function several times in a model but don't want the mutual overwriting of global/persistent variables.

A solution could be work vectors but there is little documentation about level 1 S-function work vectors. Where do i get the SimStruct S for the ssSetNumRWork function?

Thanks!

1 Answers1

1

The right thing to do is upgrade the code to be a level-2 S-Function, which shouldn't be difficult.

Level 1 m-code S-functions don't have work vectors (and all of the ssGet/Set functions are for c-code S-Functions anyway). A reason they aren't recommended any more is that they have limited functionality -- there's no nice way of getting around that without using Level 2 functionality.

You can use persistent variables (within each subfunction of a level-1 S-Function). But the point of global variables to enable them to be seen everywhere, so trying to use them without wanting everyone to see them seems pointless. (Note: you should never use global variables anyway.)

Having said that, if you really want to use Level-1 functionality, then within each S-Function you can use the getappdata and setappdata functions to act like work vector storage. But if you're going to go to that trouble, upgrading to a level-2 S-function will be easier anyway.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • OK! i'll try upgrading using this [link](http://de.mathworks.com/help/simulink/sfg/maintaining-level-1-matlab-s-functions.html#bq3i98j) - there are elementary w.v. [link](http://de.mathworks.com/help/simulink/sfg/elementary-work-vectors.html), but how to access e.g. "RWork" in matlab if ssGet/Set is for C ? – user4672571 Jul 16 '15 at 15:23
  • See the [Writing Level-2 MATLAB S-Functions](http://www.mathworks.com/help/simulink/sfg/writing-level-2-matlab-s-functions.html) doc for an example of Work Vectors and how to use them. – Phil Goddard Jul 16 '15 at 19:34
  • Is it possible that the runtime of a level-2 s-function is longer than that of a level-1 even if its the "same" code? from 1min to 2min.. – user4672571 Jul 17 '15 at 13:55
  • There is unlikely to be that much difference. – Phil Goddard Jul 17 '15 at 14:17