title: How to access (read/write) workspace variables from within S-function code?
body: <p>Is it possible to access workspace variables from within S-function code?</p>
  
  <p>Below are tested variants which are not working. There is a <code>MyVar</code> variable in <code>Model Workspace</code>, which is visible from <code>Model Explorer</code>. Lines are runned from within <code>DoPostPropSetup</code> function of custom block code.</p>
  
  <p>Appropriate error messages are in post comments</p>
  
  <pre><code>% a=evalin('Model Workspace', 'MyVar'); % Unknown command option.
  % a=evalin('caller', 'MyVar'); % (incorrect workspace)
  % a=Simulink.MyVar % Undefined variable "Simulink" or class "Simulink.MyVar".
  % a=simulink.MyVar; % Undefined variable "simulink" or class "simulink.MyVar".
  % a=Simulink.ModelWorkspace.evalin('MyVar'); % has no property or method named 'evalin'.
  </code></pre>
  
  <p>Latter is direct violation of documented facts: <a href="http://www.mathworks.com/help/simulink/slref/simulink.modelworkspace.html" rel="nofollow">http://www.mathworks.com/help/simulink/slref/simulink.modelworkspace.html</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Found something new</p>
  
  <pre><code> % a=evalin('modelworkspace', 'MyVar'); % Unknown command option.
  
     hws = get_param(bdroot, 'modelworkspace');
  %   a = hws.evalin('MyVar'); % Attempt to evaluate an expression in a read-only model workspace
  
     a = hws.getVariable('MyVar'); % works
  </code></pre>
  
  <p>latter command works, but (1) previous command said that workspace is "read only", while I need to write to variable too.</p>
  
  <p>And (2) <a href="http://www.mathworks.com/help/simulink/slref/bdroot.html?searchHighlight=bdroot" rel="nofollow"><code>bdroot</code></a> function is documented as returning "name of top-level Simulink system", which is totally unclear. I need to access workspace (not it's name) of the model, where current code' block resided.</p>