Determining variable scope, existence, and value from a called function

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Determining variable scope, existence, and value from a called function

randress
What I want to do is to call a "wrapper()" function from the MATLAB command window that:

1. determines if "fea" is in the MATLAB 'base' variable workspace.
2  if not, then calls a function to export it - the same as FEATool-->File-->Export FEA struct to MATLAB.
3. then (since it now must exist), obtain "fea" from the base workspace and assign it to "local_fea". (local_fea = fea;)
4. finally call "my_function (local_fea);"

[Note: I would just call "my_function (fea)" and then from within "my_function" see if "fea" exists using exist ('fea', 'var'), bit the call, "my_function (fea)", is refused if "fea" does not exist in the 'base' workspace.]

I am trying to make use of things like "exist" and "evalin" but so far success eludes me...

-Randal
Reply | Threaded
Open this post in threaded view
|

Re: Determining variable scope, existence, and value from a called function

Precise Simulation
Administrator
Something like this should probably work:

    function wrapper()
    
    if( ~evalin('base','exist(''fea'',''var'')') )
      h_menu_item = findall( 0, 'Label', 'Export FEA Struct To MATLAB' );
      cbf = get( h_menu_item, 'Callback' );
      cbf{1}( h_menu_item, [], cbf{2:end} );
    end
    
    local_fea = evalin('base','fea');
    
    my_function( local_fea );

Note the doubled up 'single' quotes in the evailin call to get the exist call to execute with string inputs.
Reply | Threaded
Open this post in threaded view
|

Re: Determining variable scope, existence, and value from a called function

randress
Precise Simulation wrote
Something like this should probably work:

    function wrapper()
    
    if( ~evalin('base','exist(''fea'',''var'')') )
      h_menu_item = findall( 0, 'Label', 'Export FEA Struct To MATLAB' );
      cbf = get( h_menu_item, 'Callback' );
      cbf{1}( h_menu_item, [], cbf{2:end} );
    end
    
    local_fea = evalin('base','fea');
    
    my_function( local_fea );

Note the doubled up 'single' quotes in the evailin call to get the exist call to execute with string inputs.
It does work...Many thanks!
-Randal