Switch to unified view

a b/functions/@mmo/checkworkspace.m
1
function ncopies = checkworkspace(obj);
2
3
stack = dbstack;
4
stack(1:2) = [];
5
stack      = rmfield(stack, 'line');
6
ncopies = 0;
7
if ~isempty(stack)
8
    % empty stack means the base workspace
9
    % check if we are in a different workspace
10
    if ~isequal(stack, obj.workspace)
11
        % is the current stack a subset of the obj workspace?
12
        % if yes, it means that the object was created in a subfunction
13
        % and that it is OK to modify it (because it does not exist in the
14
        % subfunction any more)
15
        subFlag = false;
16
        for index = 1:length(obj.workspace)
17
            if isequal(obj.workspace(index), stack(1))
18
                subFlag = true;
19
                if length(stack) > 1
20
                    for index2 = 1:length(obj.workspace)-index
21
                        if length(stack) < index2+1
22
                            subFlag = false;
23
                        elseif ~isequal(obj.workspace(index+index2), stack(index2+1))
24
                            subFlag = false;
25
                        end
26
                    end
27
                end
28
                if subFlag, return; end
29
            end
30
        end
31
                        
32
        % if subfunction, must be a copy
33
        if ~isempty(obj.workspace) && strcmpi(stack(end).file, obj.workspace(end).file) && ...
34
                ~strcmpi(stack(end).name, obj.workspace(end).name)
35
            % We are within a subfunction. The MMO must have
36
            % been passed as an argument (otherwise the current
37
            % workspace and the workspace variable would be
38
            % equal).
39
            ncopies = 2;
40
        else
41
            if ~isscript(stack(1).file)
42
                ncopies = 2;
43
                % we are within a function. The MMO must have
44
                % been passed as an argument (otherwise the current
45
                % workspace and the workspace variable would be
46
                % equal).
47
            else
48
                % we cannot be in a function with 0 argument
49
                % (otherwise the current workspace and the workspace
50
                % variable would be equal). We must assume that
51
                % we are in a script.
52
                while ~isempty(stack) && ~isequal(stack, obj.workspace)
53
                    stack(1) = [];
54
                end
55
                if ~isequal(stack, obj.workspace)
56
                    ncopies = 2;
57
                end
58
            end
59
        end
60
    end
61
end