Search code examples
c#c++matlabkeywordusing

Is there an equivalent for C# (or C++) keyword 'using' for Matlab?


All is said in the title.

I modified a function which was using a lot of parameters in order to give it only a structure with all these parameters inside it. Now, I don't want to rewrite everything in the function (param1 to struct.param1) so I would like to put something like 'using struct' which will permit the entire function to get 'param1' from 'struct.param1'

Is this clear or not? English is not my mother language.


Solution

  • A nasty way of doing so would be

    function my_using( params )
    %
    % params should be a struct (NOT an array of struct!)
    %
    fn = fieldnames( params );
    for ii = 1:numel( fn )
        assignin('caller', fn{ii}, params.(fn) );
    end
    

    Now you can call my_using in your function and all params in the struct will be created as variables in the function