matlabsimulinks-function

Level1 Matlab S-function - "Work Vectors"


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!


Solution

  • 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.