f



Creating Custom Workspaces

Is it posiible to create my own workspace in matlab, other than the base & the function's workspace ??

Thank you

Regards,
Pulkit
0
7/1/2009 12:31:01 AM
comp.soft-sys.matlab 211266 articles. 22 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
353 Views

Similar Articles

[PageSpeed] 15

How about this ...

function WS = Workspace(in)
    
    WS.get   = @get;
    WS.set   = @set;
    WS.clear = @clear;
    
    function out = get(arg)
        if ~ischar(arg); error('Needs to be string'); end
        try
            out = WS.(arg);
        catch
            error('Variable does not exist')
        end
        
    end
    
    function set(arg,val)
        if ~ischar(arg); error('Needs to be string'); end
        WS.(arg) = val;
    end
    
    function clear
        WS = [];
    end
    
end

Use as follows:
MyWS = Workspace;         % Create new Workspace
MyWS.set('Var1',rand(3)); % Create variable "Var1" in workspace
xyz = MyWS.get('Var1');    % Retrieve variable"Var1 from workspace
MyWS.clear()                    % Clear WS. Note that you have to use the brackets !

Just an idea with lots of room for improvement (e.g. error checking, wrap in class, ...)

Sebastian
0
7/1/2009 8:22:01 AM
Reply: