MATLAB Answers

why isn't simulink seeing a workspace variable for initial state unless it's global?

5 views (last 30 days)
jessupj
jessupj on 30 Apr 2020
Edited: jessupj on 30 Apr 2020
I have a wrapper around a simulink code that i'm calling functionally.
A dumbed down (not MWE, just an easier to present it than describe it) version looks like:
function OUTPUT = eval_simulink(X)
%...
%... relvant lines
rststate = X; % an initial state, defined from simOUT history of a previous run
SIMSYS = load_system('my_simulink_model') ;
% model containing parameters theta_1, theta_2
theta_1 = 1;
theta_2 = 1;
setActiveConfigSet( SIMSYS, 'Configuration1' );
set_param(cset,'StateSaveName','state_history')
set_param(cset, 'LoadInitialState','on');
set_param(cset, 'InitialState', 'rststate' );
%---------------
rststate
simOUT = sim( 'my_simulink_model', cset )
%--------------
OUTPUT = simOUT.state_history;
% outside this function, the input 'X' is a slice of 'OUTPUT' at some choosen point in time.
The indented lines give the following output and error respectively:
rststate =
struct with fields:
time: 108060
signals: [1×135 struct]
Error using eval_simulink (line 251)
Error evaluating expression 'rststate' for 'InitialState' specified in the Configuration Parameters dialog for
block diagram 'my_simulink_model'.
Caused by:
Error using eval_simulink (line 251)
Unrecognized function or variable 'rststate'.
Error using eval_simulink (line 251)
Variable 'rststate' does not exist.
Suggested Actions:
Load a file into base workspace. - Fix
Create a new variable. - Fix
The output immediately preceeding the error shows that the variable rststate is defined, but the error seems to state that it doesn't exist.
This seems to be a local scope problem ie: because rststate is defined within a function, the local workspace of the compiled model can't see it? Other models/wrappers designed similarly didn't give this error. I can declare rststate as a global wihtin the m-file, and this works fine.
Why are other paramters defined locally within the funciton like theta_i passed to it successfully?

Answers (0)

Community Treasure Hunt

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

Start Hunting!