This is a static copy of a profile report

Home

workspacefunc (2 calls, 0.000 sec)
Generated 14-Nov-2016 07:47:18 using cpu time.
function in file /usr/local/MATLAB/MATLAB_Production_Server/R2015a/toolbox/matlab/codetools/workspacefunc.m
Copy to new window for comparing multiple runs

Parents (calling functions)
No parent
Lines where the most time was spent
No measurable time spent in this function

Line NumberCodeCallsTotal Time% TimeTime Plot
55
return
20 s0%
54
varargout = {getWhosInformatio...
20 s0%
53
case 'getwhosinformation',
20 s0%
49
case 'getworkspaceid'
20 s0%
45
case 'getworkspace'
20 s0%
All other lines  0 s0%
Totals  0 s0% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
onCleanup>onCleanup.deletesubfunction20 s0%
workspacefunc>getWhosInformationsubfunction20 s0%
workspacefunc>getCleanupHandlersubfunction20 s0%
Self time (built-ins, overhead, etc.)  0 s0%
Totals  0 s0% 
Code Analyzer results
Line numberMessage
Coverage results
Show coverage for parent directory
Total lines in function509
Non-code lines (comments, blank lines)162
Code lines (lines that can run)347
Code lines that did run17
Code lines that did not run330
Coverage (did run/can run)4.90 %
Function listing
time 
calls 
 line
   1 
function varargout = workspacefunc(whichcall, varargin)
   2 
%WORKSPACEFUNC  Support function for Workspace browser component.
   3 

   4 
% Copyright 1984-2014 The MathWorks, Inc.
   5 

   6 
% make sure this function does not clear
      2 
   7 
mlock  
   8 

      2 
   9 
persistent defaultWorkspaceID simulinkLoaded 
      2 
  10 
if isempty(defaultWorkspaceID) 
  11 
    defaultWorkspaceID = getDefaultWorkspaceID;
  12 
end
      2 
  13 
if isempty(simulinkLoaded) 
  14 
    simulinkLoaded = false;
  15 
end
  16 

  17 
% throttle workspace updates if any simulations are running
  18 
% let the first one go then reset the long delay after the 
  19 
% next event
      2 
  20 
if simulinkLoaded || inmem('-isloaded','simulink') 
  21 
    simulinkLoaded = true;
  22 
    % simulink is loaded - are any systems open and running?
  23 
    statuses = get_param(find_system(0,'type','block_diagram'),'SimulationStatus');
  24 
    if ~isempty(statuses)
  25 
        if iscell(statuses)
  26 
            running = any(cellfun(@(x)~isempty(x),strfind(statuses,'running')));
  27 
        else
  28 
            running = isequal(statuses,'running');
  29 
        end
  30 
        com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.setSimulationRunning(running);
  31 
    end
  32 
end
  33 

  34 
% Special handling for some of the updated APIs
      2 
  35 
switch whichcall 
      2 
  36 
    case 'getdefaultworkspaceid', 
  37 
        varargout{1} = defaultWorkspaceID;
  38 
        return
  39 

      2 
  40 
    case {'getworkspace','getworkspaceid','getwhosinformation'} 
      2 
  41 
        swl = com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.swl(true); 
      2 
  42 
        cleaner = getCleanupHandler(swl); %#ok<NASGU> 
      2 
  43 
        try 
      2 
  44 
            switch whichcall 
      2 
  45 
                case 'getworkspace' 
  46 
                    varargout{1} = getWorkspace(varargin{1});
  47 
                    return
  48 
                    
      2 
  49 
                case 'getworkspaceid' 
  50 
                    varargout{1} = setWorkspace(varargin{1});
  51 
                    return
  52 
                    
      2 
  53 
                case 'getwhosinformation', 
      2 
  54 
                    varargout = {getWhosInformation(varargin{1})}; 
      2 
  55 
                    return 
  56 
            end
  57 
        catch e
  58 
            showError(whichcall, e)
  59 
            varargout{1} = [];
  60 
            return
  61 
        end
  62 
end
  63 

  64 
if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
  65 
    swl = com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.swl(true);
  66 
    cleaner = getCleanupHandler(swl); %#ok<NASGU>
  67 
end
  68 

  69 
% some callers expect to handle errors as strings in varargout
  70 
returnError = false;
  71 

  72 
% some callers expect to handle errors as thrown errors
  73 
throwError = true;
  74 

  75 
% some callers require the VariableEditor to refresh.
  76 
requireUpdate = false;
  77 

  78 
if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
  79 
    % Note: the length() == 1 test is for 'save'.
  80 
    variable = varargin{1};
  81 
    variableName = char(variable.getVariableName);
  82 
    workspaceID = variable.getWorkspaceID;
  83 
    theWorkspace = workspacefunc('getworkspace', workspaceID);
  84 
    
  85 
    % always check for base name
  86 
    baseVariableName = arrayviewfunc('getBaseVariableName',variableName);
  87 
    if ischar(theWorkspace)
  88 
        exists = logical(evalin(theWorkspace, ['builtin(''exist'',''' baseVariableName ''' , ''var'')']));
  89 
    else
  90 
        exists = logical(evalin(theWorkspace, ['exist( ''' baseVariableName ''' , ''var'')']));
  91 
    end
  92 
    if exists
  93 
        try
  94 
            baseValue = evalin(theWorkspace, baseVariableName);
  95 
            currentValue = evalin(theWorkspace, variableName);
  96 
        catch e
  97 
            showError(whichcall, e)
  98 
            currentValue = [];
  99 
        end
 100 
    else
 101 
        currentValue = [];
 102 
    end
 103 
end
 104 

 105 
try
 106 
    
 107 
switch whichcall
 108 
    case 'getcopyname',
 109 
        varargout = {getCopyName(varargin{1}, varargin{2})};
 110 
    case 'getnewname',
 111 
        varargout = {getNewName(varargin{1})};
 112 
    case 'getshortvalue',
 113 
        varargout = {getShortValue(varargin{1})};
 114 
    case 'getshortvalues',
 115 
        getShortValues(varargin{1});
 116 
    case 'getshortvalueserror',
 117 
        getShortValuesError(varargin{1});
 118 
    case 'getshortvalueobjectj',
 119 
        varargout = {getShortValueObjectJ(varargin{1}, varargin{2:end})};
 120 
    case 'getshortvaluesbypropj',
 121 
        varargout = {getShortValuesByPropJ(varargin{:})};
 122 
    case 'num2complex',
 123 
        varargout = {num2complex(varargin{1}, varargin{2:end})};
 124 
    case 'getshortvalueobjectsj',
 125 
        varargout = {getShortValueObjectsJ(varargin{1})};
 126 
    case 'getshortvalueobjectswithevalj',
 127 
        vars = varargin{1};
 128 
        w = warning('off', 'all');
 129 
        ret = javaArray('java.lang.Object', length(vars));
 130 
        for i = 1:length(vars)
 131 
            try
 132 
                ret(i) = getShortValueObjectJ(evalin('caller',vars{i}));
 133 
            catch err %#ok<NASGU>
 134 
                ret(i) = java.lang.String(getString(message('MATLAB:codetools:workspacefunc:ErrorDisplayingValue')));
 135 
            end
 136 
        end
 137 
        warning(w);
 138 
        varargout{1} = ret;
 139 
    case 'getabstractvaluesummariesj',
 140 
        varargout = {getAbstractValueSummariesJ(varargin{1})};
 141 
    case 'getabstractvaluesummariesjnames'
 142 
        w = warning('off', 'all');
 143 
        vars = varargin{1};
 144 
        ret = javaArray('java.lang.Object', length(vars));
 145 
        for i = 1:length(vars)
 146 
            try
 147 
                % try to eval the variable here, instead of when calling
 148 
                % the function, so that we can catch any errors and prevent
 149 
                % error beeping
 150 
                v = evalin('caller', vars{i});
 151 
                ret(i) = getAbstractValueSummaryJ(v);
 152 
            catch err %#ok<NASGU>
 153 
                ret(i) = java.lang.String(getString(message('MATLAB:codetools:workspacefunc:ErrorDisplayingValue')));
 154 
            end
 155 
        end
 156 
        varargout = {ret};
 157 
        warning(w);
 158 
    case 'getstatobjectm',
 159 
        varargout = {getStatObjectM(varargin{1}, varargin{2}, varargin{3})};
 160 
    case 'getstatobjectsj',
 161 
        varargout = {getStatObjectsJ(varargin{1}, varargin{2}, varargin{3}, varargin{4})};
 162 
    case 'getshortvalueerrorobjects',
 163 
        varargout = {getShortValueErrorObjects(varargin{1})};
 164 
    case 'areAnyVariablesReadOnly',
 165 
        varargout = {areAnyVariablesReadOnly(varargin{1}, varargin{2})};
 166 

 167 
    % New APIs begin here
 168 
    case 'getExist'
 169 
        throwError = false;
 170 

 171 
        varargout{1} = exists;
 172 
        
 173 
    case 'save'
 174 
        returnError = true;
 175 
        
 176 
        variables = varargin{1};
 177 
        fullPath = char(varargin{2});
 178 
        doAppend = varargin{3};
 179 
        if isempty(variables)
 180 
            workspaceID = getDefaultWorkspaceID;
 181 
            variableNames = '';
 182 
        else
 183 
            workspaceID = variables{1}.getWorkspaceID;
 184 
            variableNames = char(variables{1}.getVariableName);
 185 
            for i = 2:length(variables)
 186 
                if (variables{i} ~= workspaceID)
 187 
                    error(message('MATLAB:codetools:workspacefunc:AllVariablesMustHaveSameID'));
 188 
                end
 189 
                variableNames = [variableNames ' ' char(variables{1}.getVariableName)]; %#ok<AGROW>
 190 
            end
 191 
        end
 192 
        
 193 
        appendStr = '';
 194 
        if doAppend
 195 
            appendStr = ' -append';
 196 
        end
 197 
        
 198 
        evalin(getWorkspace(workspaceID), ['save ' fullPath ' ' variableNames appendStr]);
 199 
        
 200 
    case 'whos'
 201 
        throwError = false;
 202 

 203 
        evalin(getWorkspace(varargin{1}), 'builtin(''whos'')')
 204 
        
 205 
    case 'getshortvalueobjects',
 206 
        throwError = true;
 207 

 208 
        workspaceID = varargin{1};
 209 
        varargin = varargin(2:end);
 210 
        
 211 
        values = varargin{1};
 212 
        for i = 1:length(values)
 213 
            values{i} = evalin(getWorkspace(workspaceID),values{i});
 214 
        end
 215 
        varargout = {getShortValueObjectsJ(values)};
 216 
        
 217 
        
 218 
    case 'getshortvaluesbyprop',
 219 
        throwError = true;
 220 

 221 
        workspaceID = varargin{1};
 222 
        varargin = varargin(2:end);
 223 
        
 224 
        props = varargin{1};
 225 
        varName = varargin{2};
 226 
        values = cell(1,length(props));
 227 
        for i = 1:length(values)
 228 
            try
 229 
                values{i} = evalin(getWorkspace(workspaceID),sprintf('%s.%s',varName,props{i}));
 230 
            catch
 231 
                values{i} = [];
 232 
            end
 233 
        end
 234 
        varargout = {getShortValueObjectsJ(values)};
 235 
        
 236 
    case 'getabstractvaluesummaries',
 237 
        throwError = true;
 238 

 239 
        varargout = {getAbstractValueSummariesJ({currentValue})};
 240 

 241 
    case 'getvariableclass'
 242 
        throwError = true;
 243 
        try
 244 
            if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
 245 
                varargout = {class(currentValue)};
 246 
            else
 247 
                % try to eval the variable here, instead of when calling
 248 
                % the function, so that we can catch any errors and prevent
 249 
                % error beeping
 250 
                v = evalin('caller', varargin{1});
 251 
                varargout = {class(v)};
 252 
            end
 253 
        catch
 254 
            varargout = {''};
 255 
        end
 256 
        
 257 
    case 'getvariablestatobjects',
 258 
        throwError = true;
 259 

 260 
        workspaceID = varargin{1};
 261 
        theWorkspace = getWorkspace(workspaceID);
 262 
        
 263 
        values = varargin{2};
 264 
        for i = 1:length(values)
 265 
            variableName = values{i};
 266 
            currentValue = evalin(theWorkspace, variableName);
 267 
            values{i} = currentValue;
 268 
        end
 269 
        varargin = varargin(3:end);
 270 
        varargout = {getStatObjectsJ(values, varargin{1}, varargin{2}, varargin{3})};
 271 
        
 272 
    case 'whosinformation',
 273 
        throwError = false;
 274 
        if varargin{1} == getDefaultWorkspaceID
 275 
            error(message('MATLAB:codetools:workspacefunc:GettingVariablesfromDefaultWorkspaceNotImplemented'));
 276 
        end
 277 
        varargout = {getWhosInformation(evalin(getWorkspace(varargin{1}),'builtin(''whos'')'))};
 278 
        
 279 
    case 'readonly',
 280 
        throwError = false;
 281 

 282 
        theWorkspace = getWorkspace(varargin{1});
 283 
        variableName = varargin{2};
 284 

 285 
        %split and join are used to escape all quotes in the variable name
 286 
        if ischar(theWorkspace)
 287 
            exists = logical(evalin(theWorkspace, ['builtin(''exist'',''' strjoin(strsplit(variableName,''''),'''''') ''', ''var'')']));
 288 
        else
 289 
            exists = logical(evalin(theWorkspace, ['exist(''' strjoin(strsplit(variableName,''''),'''''') ''' , ''var'')']));
 290 
        end
 291 
        if ~exists
 292 
            varargout{1} = false;
 293 
            return
 294 
        end
 295 
        
 296 
        varargin = varargin(2:end);
 297 
        names = varargin{2};
 298 
        values = varargin{2};
 299 
        for i = 1:length(values)
 300 
            variableName = values{i};
 301 
            currentValue = evalin(theWorkspace, variableName);
 302 
            values{i} = currentValue;
 303 
        end
 304 
        varargout = {areAnyVariablesReadOnly(values, names)};
 305 
        
 306 
    case 'rmfield'
 307 
        throwError = false;
 308 

 309 
        % cannot use assignin because variable name can itself
 310 
        % be a struct field
 311 
        expr = [variableName ' = rmfield(' variableName ','''...
 312 
            varargin{2} ''');'];
 313 
        evalin(theWorkspace, expr);
 314 
        requireUpdate = true;
 315 
        
 316 
    case 'renamefield',
 317 
        throwError = false;
 318 
        % cannot use assignin because variable name can itself
 319 
        % be a struct field
 320 
        expr = [variableName ' = renameStructField(' variableName ','''...
 321 
            varargin{2} ''',''' varargin{3} ''');'];
 322 
        evalin(theWorkspace, expr);
 323 
        requireUpdate = true;
 324 
        
 325 
    case 'createUniqueField',
 326 
        % Create new field in scalar struct, struct array, or nested
 327 
        % structs.
 328 
        throwError = false;
 329 
        
 330 
        %
 331 
        % Uniquely named field (unnamed#) is added at the end, and assigned
 332 
        % a value of 0.
 333 
        %
 334 
        fields = evalin(theWorkspace,['fieldnames(' variableName ')']);
 335 
        fieldLength = length(fields) + 1;
 336 
        newName = getNewName(fields);
 337 
        
 338 
        %
 339 
        % determine the new order of the fields, if a 'insert before' field
 340 
        % isn't specified, insert it at the beginning
 341 
        %
 342 
        if (numel(varargin) == 2) 
 343 
            insertBefore = varargin{2};
 344 
            index = find(strcmp(insertBefore, fields));
 345 
        else
 346 
            index = [];
 347 
        end
 348 
        
 349 
        %
 350 
        % create the strings that will define the new field order - note
 351 
        % that arrays aren't used so as to minimize the length of the input
 352 
        % string to evalin below
 353 
        %
 354 
        if (~isempty(index))
 355 
            % fieldOrder = [1:index-1 fieldLength index:fieldLength-1];
 356 
            fieldOrder = sprintf('[1:%d %d %d:%d]', index-1, fieldLength, index, fieldLength-1);
 357 
        else
 358 
            % fieldOrder = [1:fieldLength];
 359 
            fieldOrder = sprintf('[1:%d]', fieldLength);
 360 
        end
 361 
        
 362 
        % first add a field
 363 
        evalin(theWorkspace,sprintf('[%s.(''%s'')]=deal(0);', variableName, newName));
 364 
        
 365 
        % now reorder them (get rid of any indexing at the end as it is illegal
 366 
        % to modify an element of a struct array with disimilarly structured
 367 
        % elements
 368 
        variableName = regexprep(variableName,'\(.+\)$','');
 369 
        evalin(theWorkspace,sprintf('%s=orderfields(%s,%s);', variableName, variableName, fieldOrder));
 370 

 371 
        requireUpdate = true;
 372 
        
 373 
    case 'duplicateField',
 374 
        throwError = false;
 375 

 376 
        fields = fieldnames(currentValue);
 377 
        newName = getCopyName(varargin{2}, fields);
 378 
        % cannot use assignin because variable name can itself
 379 
        % be a struct field
 380 
        expr = [variableName '.' newName ' = ' variableName '.' ...
 381 
            varargin{2} ';'];
 382 
        evalin(theWorkspace, expr);
 383 
        requireUpdate = true;
 384 
 
 385 
    case 'createVariable'
 386 
        throwError = false;
 387 

 388 
        expression = varargin{2};
 389 
        newValue = evalin(theWorkspace,expression);
 390 
        whoOutput = evalin(theWorkspace,'who');
 391 
        newName = getNewName(whoOutput);
 392 
        assignin(theWorkspace, newName, newValue)
 393 
        requireUpdate = true;
 394 
        
 395 
    case 'assignVariable'
 396 
        throwError = false;
 397 

 398 
        returnError = true;
 399 
        expression = varargin{2};
 400 
        newExpression = evalin(theWorkspace,expression);
 401 
        newValue = setVariableValue(baseVariableName, baseValue, variableName, newExpression);
 402 
        assignin(theWorkspace, baseVariableName, newValue)
 403 
        requireUpdate = true;
 404 
    
 405 
    case 'copyworkspacevariables'
 406 
        % workspacefunc('copyworkspacevariables', <cell array of variable names>, <cell array of variable identifiers>)
 407 
        % Variable Names - Used to store and retrieve the variables from storage.
 408 
        % Variable Identifiers - Used to query the values of variables from the caller workspace. 
 409 
        
 410 
        % Set required variables
 411 
        varargout = {};
 412 
        
 413 
        if isempty(varargin{1}) || isempty(varargin{2})
 414 
            return
 415 
        end
 416 
        
 417 
        % Set variable names and identifiers from varargin
 418 
        varNames = varargin{1};       % cell array of variable names
 419 
        varIdentifiers = varargin{2}; % cell array of variable identifiers
 420 
        
 421 
        % Set variable names in storage
 422 
        copyPasteStorage('-setVarNames',varNames);
 423 
        
 424 
        % Get variable values from caller workspace and pass into storage
 425 
        evalin('caller',[mfilename,...
 426 
            '(''setcopyvarvalues'',{',cell2csv(varIdentifiers),'});']);
 427 
        
 428 
    case 'pasteworkspacevariables'
 429 
        % workspacefunc('pasteworkspacevariables', <cell array of original variable names>, <cell array of new variable names>)
 430 
        % Original Names - Used to query variables in storage
 431 
        % New Names - Used to name variables in Caller workspace
 432 
        
 433 
        % Set required variables
 434 
        varargout = {};
 435 
        
 436 
        if isempty(varargin{1}) || isempty(varargin{2})
 437 
            return
 438 
        end
 439 
        
 440 
        % Get cell array of stored and new variable names
 441 
        origNames = varargin{1};   % cell array of original variable names        
 442 
        newNames  = varargin{2};   % cell array of new variable names
 443 
        
 444 
        % Clean up any leaked variables
 445 
        copyPasteStorage('-clearVariables',origNames);
 446 
        
 447 
        % Check to be sure that 'copy' has been called previously
 448 
        if    copyPasteStorage('-isempty')
 449 
            return;
 450 
        end
 451 

 452 
        % Create variables in caller workspace
 453 
        for varNameIdx = 1:numel(newNames)
 454 
            evalin('caller',[newNames{varNameIdx},...
 455 
                ' = ',mfilename,'(''getcopyvarvalues'',''',...
 456 
                origNames{varNameIdx},''');']);
 457 
        end
 458 
        
 459 
        requireUpdate = true;
 460 

 461 
    case 'clearstoredworkspacevariables'
 462 
        % workspacefunc('clearstoredworkspacevariables', <cell array of variables to be saved>{optional})
 463 
        % Saved Variables - Optional way of preventing certain variables from being cleared from storage.
 464 
        
 465 
        % Set required variables
 466 
        varargout = {};
 467 
        
 468 
        % Clean up variable storage
 469 
        if isempty(varargin)
 470 
            copyPasteStorage('-clearAllVariables');
 471 
        else
 472 
            copyPasteStorage('-clearVariables',varargin{1});
 473 
        end
 474 
        
 475 
    case 'getcopyvarnames'
 476 
        % Return names of all variables in storage (Used for testing)
 477 
        varargout = {copyPasteStorage('-getVarNames')}; 
 478 

 479 
    % The following 2 cases are called from previous cases and require data 
 480 
    % to be input from the caller workspace    
 481 
    case 'setcopyvarvalues'
 482 
        % Set required variables
 483 
        varargout = {};
 484 
        
 485 
        % Set variable values in storage
 486 
        copyPasteStorage('-setVarValues',varargin{1});
 487 
        
 488 
    case 'getcopyvarvalues'
 489 
        % Return variable values from storage
 490 
        varargout = {copyPasteStorage('-getVarValues',varargin{1})};
 491 
            
 492 
    otherwise
 493 
        error(message('MATLAB:workspacefunc:unknownOption'));
 494 
end
 495 

 496 
    if requireUpdate
 497 
        com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.reportWSChange();
 498 
    end
 499 
    
 500 
catch e
 501 
    if returnError
 502 
        varargout{1} = e.message;
 503 
    elseif throwError
 504 
        rethrow(e)
 505 
    else
 506 
        showError(whichcall, e)
 507 
        varargout{1}=[]; % to suppress hard error message display on command window
 508 
    end
 509 
end

Other subfunctions in this file are not included in this listing.