time | Calls | line |
|---|
| | 1 | function varargout = workspacefunc(whichcall, varargin)
|
| | 2 | %WORKSPACEFUNC Support function for Workspace browser component.
|
| | 3 |
|
| | 4 | % Copyright 1984-2018 The MathWorks, Inc.
|
| | 5 |
|
| | 6 | % make sure this function does not clear
|
< 0.001 | 3 | 7 | mlock
|
| | 8 |
|
< 0.001 | 3 | 9 | persistent defaultWorkspaceID lastUpdate
|
< 0.001 | 3 | 10 | if isempty(defaultWorkspaceID)
|
| | 11 | defaultWorkspaceID = getDefaultWorkspaceID;
|
| 3 | 12 | end
|
| | 13 |
|
| | 14 | % Special handling for some of the updated APIs
|
< 0.001 | 3 | 15 | switch whichcall
|
< 0.001 | 3 | 16 | case 'getlastupdate'
|
| | 17 | if isempty(lastUpdate)
|
| | 18 | error('Workspace browser is not up to date - perhaps it''s not open')
|
| | 19 | end
|
| | 20 | varargout{1} = lastUpdate;
|
| | 21 | return
|
< 0.001 | 3 | 22 | case 'requestupdate'
|
| | 23 | reportWSChange
|
| | 24 | return
|
| 3 | 25 | end
|
| | 26 |
|
| | 27 | % Update this value to easily track when the workspace browser is updated
|
0.004 | 3 | 28 | lastUpdate = now;
|
| | 29 |
|
| | 30 | % Special handling for some of the updated APIs
|
< 0.001 | 3 | 31 | switch whichcall
|
< 0.001 | 3 | 32 | case 'getdefaultworkspaceid'
|
| | 33 | varargout{1} = defaultWorkspaceID;
|
| | 34 | return
|
| | 35 |
|
< 0.001 | 3 | 36 | case {'getworkspace','getworkspaceid','getwhosinformation'}
|
< 0.001 | 3 | 37 | swl = com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.swl(true);
|
0.002 | 3 | 38 | cleaner = getCleanupHandler(swl); %#ok<NASGU>
|
| 3 | 39 | try
|
| 3 | 40 | switch whichcall
|
< 0.001 | 3 | 41 | case 'getworkspace'
|
| | 42 | varargout{1} = getWorkspace(varargin{1});
|
| | 43 | return
|
| | 44 |
|
| 3 | 45 | case 'getworkspaceid'
|
| | 46 | varargout{1} = setWorkspace(varargin{1});
|
| | 47 | return
|
| | 48 |
|
< 0.001 | 3 | 49 | case 'getwhosinformation'
|
0.001 | 3 | 50 | varargout = {getWhosInformation(varargin{1})};
|
0.001 | 3 | 51 | return
|
| | 52 | end
|
| | 53 | catch e
|
| | 54 | if nargin < 3 || ~islogical(varargin{2}) || varargin{2}
|
| | 55 | showError(whichcall, e)
|
| | 56 | end
|
| | 57 | varargout{1} = [];
|
| | 58 | return
|
| | 59 | end
|
| | 60 | end
|
| | 61 |
|
| | 62 | if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
|
| | 63 | swl = com.mathworks.mlwidgets.workspace.MatlabWorkspaceListener.swl(true);
|
| | 64 | cleaner = getCleanupHandler(swl); %#ok<NASGU>
|
| | 65 | end
|
| | 66 |
|
| | 67 | % some callers expect to handle errors as strings in varargout
|
| | 68 | returnError = false;
|
| | 69 |
|
| | 70 | % some callers expect to handle errors as thrown errors
|
| | 71 | throwError = true;
|
| | 72 |
|
| | 73 | % some callers require the VariableEditor to refresh.
|
| | 74 | requireUpdate = false;
|
| | 75 |
|
| | 76 | if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
|
| | 77 | % Note: the length() == 1 test is for 'save'.
|
| | 78 | variable = varargin{1};
|
| | 79 | variableName = char(variable.getVariableName);
|
| | 80 | workspaceID = variable.getWorkspaceID;
|
| | 81 | theWorkspace = workspacefunc('getworkspace', workspaceID);
|
| | 82 |
|
| | 83 | % always check for base name
|
| | 84 | baseVariableName = arrayviewfunc('getBaseVariableName',variableName);
|
| | 85 | exists = [];
|
| | 86 |
|
| | 87 | if ischar(theWorkspace)
|
| | 88 | exists = logical(evalin(theWorkspace, ['builtin(''exist'',''' baseVariableName ''' , ''var'')']));
|
| | 89 | elseif ismethod(theWorkspace, 'hasVariable')
|
| | 90 | try
|
| | 91 | exists = hasVariable(theWorkspace, baseVariableName);
|
| | 92 | catch
|
| | 93 | end
|
| | 94 | end
|
| | 95 |
|
| | 96 | if isempty(exists)
|
| | 97 | exists = logical(evalin(theWorkspace, ['exist( ''' baseVariableName ''' , ''var'')']));
|
| | 98 | end
|
| | 99 | if exists
|
| | 100 | try
|
| | 101 | if ismethod(theWorkspace, 'getVariable')
|
| | 102 | baseValue = getVariable(theWorkspace, baseVariableName);
|
| | 103 |
|
| | 104 | if ~isequal(baseVariableName, variableName)
|
| | 105 | % getVariable only works on the base variable name (you
|
| | 106 | % can't access struct field values, for example), so
|
| | 107 | % need to fallback to using evalin
|
| | 108 | currentValue = evalin(theWorkspace, variableName);
|
| | 109 | else
|
| | 110 | try
|
| | 111 | % Use the getVariable method on the workspace to
|
| | 112 | % get access to the variable
|
| | 113 | currentValue = getVariable(theWorkspace, variableName);
|
| | 114 | catch
|
| | 115 | % In case of failure, fallback to using evalin
|
| | 116 | currentValue = evalin(theWorkspace, variableName);
|
| | 117 | end
|
| | 118 | end
|
| | 119 | else
|
| | 120 | baseValue = evalin(theWorkspace, baseVariableName);
|
| | 121 | currentValue = evalin(theWorkspace, variableName);
|
| | 122 | end
|
| | 123 | catch e
|
| | 124 | showError(whichcall, e)
|
| | 125 | currentValue = [];
|
| | 126 | end
|
| | 127 | else
|
| | 128 | currentValue = [];
|
| | 129 | end
|
| | 130 | end
|
| | 131 |
|
| | 132 | try
|
| | 133 |
|
| | 134 | switch whichcall
|
| | 135 | case 'getcopyname'
|
| | 136 | varargout = {getCopyName(varargin{1}, varargin{2})};
|
| | 137 | case 'getnewname'
|
| | 138 | varargout = {getNewName(varargin{1})};
|
| | 139 | case 'getshortvalue'
|
| | 140 | varargout = {getShortValue(varargin{1})};
|
| | 141 | case 'getshortvalues'
|
| | 142 | getShortValues(varargin{1});
|
| | 143 | case 'getshortvalueserror'
|
| | 144 | getShortValuesError(varargin{1});
|
| | 145 | case 'getshortvalueobjectj'
|
| | 146 | varargout = {getShortValueObjectJ(varargin{1}, varargin{2:end})};
|
| | 147 | case 'getshortvaluesbypropj'
|
| | 148 | varargout = {getShortValuesByPropJ(varargin{:})};
|
| | 149 | case 'num2complex'
|
| | 150 | varargout = {num2complex(varargin{1}, varargin{2:end})};
|
| | 151 | case 'getshortvalueobjectsj'
|
| | 152 | varargout = {getShortValueObjectsJ(varargin{1})};
|
| | 153 | case 'getshortvalueobjectswithevalj'
|
| | 154 | vars = varargin{1};
|
| | 155 | w = warning('off', 'all');
|
| | 156 | ret = javaArray('java.lang.Object', length(vars));
|
| | 157 | for i = 1:length(vars)
|
| | 158 | try
|
| | 159 | ret(i) = getShortValueObjectJ(evalin('caller',vars{i}));
|
| | 160 | catch err %#ok<NASGU>
|
| | 161 | ret(i) = java.lang.String(getString(message('MATLAB:codetools:workspacefunc:ErrorDisplayingValue')));
|
| | 162 | end
|
| | 163 | end
|
| | 164 | warning(w);
|
| | 165 | varargout{1} = ret;
|
| | 166 | case 'getabstractvaluesummariesj'
|
| | 167 | varargout = {getAbstractValueSummariesJ(varargin{1})};
|
| | 168 | case 'getabstractvaluesummariesjnames'
|
| | 169 | w = warning('off', 'all');
|
| | 170 | vars = varargin{1};
|
| | 171 | ret = javaArray('java.lang.Object', length(vars));
|
| | 172 | for i = 1:length(vars)
|
| | 173 | try
|
| | 174 | % Check to see if the variable exists in the calling
|
| | 175 | % workspace before trying to evaluate it
|
| | 176 | if evalin('caller',['exist(''' vars{i} ''',''var'')'])
|
| | 177 | % try to eval the variable here, instead of when calling
|
| | 178 | % the function, so that we can catch any errors and prevent
|
| | 179 | % error beeping
|
| | 180 | v = evalin('caller', vars{i});
|
| | 181 | ret(i) = getAbstractValueSummaryJ(v);
|
| | 182 | else
|
| | 183 | ret(i) = java.lang.String(getString(message('MATLAB:codetools:workspacefunc:ErrorDisplayingValue')));
|
| | 184 | end
|
| | 185 | catch err %#ok<NASGU>
|
| | 186 | ret(i) = java.lang.String(getString(message('MATLAB:codetools:workspacefunc:ErrorDisplayingValue')));
|
| | 187 | end
|
| | 188 | end
|
| | 189 | varargout = {ret};
|
| | 190 | warning(w);
|
| | 191 | case 'getstatobjectm'
|
| | 192 | varargout = {getStatObjectM(varargin{1}, varargin{2}, varargin{3})};
|
| | 193 | case 'getstatobjectsj'
|
| | 194 | w = warning('off', 'all');
|
| | 195 | vars = varargin{1};
|
| | 196 | baseFunction = varargin{2};
|
| | 197 | showNaNs = varargin{3};
|
| | 198 | numelLimit = varargin{4};
|
| | 199 |
|
| | 200 | ret = javaArray('java.lang.Object', length(vars));
|
| | 201 | for i = 1:length(vars)
|
| | 202 | try
|
| | 203 | % Check to see if the variable exists in the calling
|
| | 204 | % workspace before trying to evaluate it
|
| | 205 | baseVarName = arrayviewfunc('getBaseVariableName', vars{i});
|
| | 206 | if evalin('caller',['exist(''' baseVarName ''',''var'')'])
|
| | 207 | % try to eval the variable here, instead of when calling
|
| | 208 | % the function, so that we can catch any errors and prevent
|
| | 209 | % error beeping
|
| | 210 | v = evalin('caller', vars{i});
|
| | 211 | ret(i) = getStatObjectJVar(v, baseFunction, showNaNs, numelLimit);
|
| | 212 | else
|
| | 213 | ret(i) = java.lang.String('');
|
| | 214 | end
|
| | 215 | catch err %#ok<NASGU>
|
| | 216 | ret(i) = java.lang.String('');
|
| | 217 | end
|
| | 218 | end
|
| | 219 | varargout = {ret};
|
| | 220 | warning(w);
|
| | 221 |
|
| | 222 | case 'getshortvalueerrorobjects'
|
| | 223 | varargout = {getShortValueErrorObjects(varargin{1})};
|
| | 224 | case 'areAnyVariablesReadOnly'
|
| | 225 | varargout = {areAnyVariablesReadOnly(varargin{1}, varargin{2})};
|
| | 226 |
|
| | 227 | % New APIs begin here
|
| | 228 | case 'getExist'
|
| | 229 | throwError = false;
|
| | 230 |
|
| | 231 | varargout{1} = exists;
|
| | 232 |
|
| | 233 | case 'save'
|
| | 234 | returnError = true;
|
| | 235 |
|
| | 236 | variables = varargin{1};
|
| | 237 | fullPath = char(varargin{2});
|
| | 238 | doAppend = varargin{3};
|
| | 239 | if isempty(variables)
|
| | 240 | workspaceID = getDefaultWorkspaceID;
|
| | 241 | variableNames = '';
|
| | 242 | else
|
| | 243 | workspaceID = variables{1}.getWorkspaceID;
|
| | 244 | variableNames = char(variables{1}.getVariableName);
|
| | 245 | for i = 2:length(variables)
|
| | 246 | if (variables{i} ~= workspaceID)
|
| | 247 | error(message('MATLAB:codetools:workspacefunc:AllVariablesMustHaveSameID'));
|
| | 248 | end
|
| | 249 | variableNames = [variableNames ' ' char(variables{1}.getVariableName)]; %#ok<AGROW>
|
| | 250 | end
|
| | 251 | end
|
| | 252 |
|
| | 253 | appendStr = '';
|
| | 254 | if doAppend
|
| | 255 | appendStr = ' -append';
|
| | 256 | end
|
| | 257 |
|
| | 258 | evalin(getWorkspace(workspaceID), ['save ' fullPath ' ' variableNames appendStr]);
|
| | 259 |
|
| | 260 | case 'whos'
|
| | 261 | throwError = false;
|
| | 262 |
|
| | 263 | evalin(getWorkspace(varargin{1}), 'builtin(''whos'')')
|
| | 264 |
|
| | 265 | case 'getshortvalueobjects'
|
| | 266 | throwError = true;
|
| | 267 |
|
| | 268 | workspaceID = varargin{1};
|
| | 269 | varargin = varargin(2:end);
|
| | 270 |
|
| | 271 | values = varargin{1};
|
| | 272 | for i = 1:length(values)
|
| | 273 | values{i} = evalin(getWorkspace(workspaceID),values{i});
|
| | 274 | end
|
| | 275 | varargout = {getShortValueObjectsJ(values)};
|
| | 276 |
|
| | 277 |
|
| | 278 | case 'getshortvaluesbyprop'
|
| | 279 | throwError = true;
|
| | 280 |
|
| | 281 | workspaceID = varargin{1};
|
| | 282 | varargin = varargin(2:end);
|
| | 283 |
|
| | 284 | props = varargin{1};
|
| | 285 | varName = varargin{2};
|
| | 286 | values = cell(1,length(props));
|
| | 287 | for i = 1:length(values)
|
| | 288 | try
|
| | 289 | values{i} = evalin(getWorkspace(workspaceID),sprintf('%s.%s',varName,props{i}));
|
| | 290 | catch
|
| | 291 | values{i} = [];
|
| | 292 | end
|
| | 293 | end
|
| | 294 | varargout = {getShortValueObjectsJ(values)};
|
| | 295 |
|
| | 296 | case 'getabstractvaluesummaries'
|
| | 297 | throwError = true;
|
| | 298 |
|
| | 299 | varargout = {getAbstractValueSummariesJ({currentValue})};
|
| | 300 |
|
| | 301 | case 'getvariableclass'
|
| | 302 | throwError = true;
|
| | 303 | try
|
| | 304 | if nargin > 1 && isa(varargin{1},'com.mathworks.mlservices.WorkspaceVariable') && length(varargin{1}) == 1
|
| | 305 | varargout = {class(currentValue)};
|
| | 306 | elseif nargin > 1
|
| | 307 | % try to eval the variable here, instead of when calling
|
| | 308 | % the function, so that we can catch any errors and prevent
|
| | 309 | % error beeping
|
| | 310 | v = evalin('caller', varargin{1});
|
| | 311 | varargout = {class(v)};
|
| | 312 | else
|
| | 313 | varargout = {''};
|
| | 314 | end
|
| | 315 | catch
|
| | 316 | varargout = {''};
|
| | 317 | end
|
| | 318 |
|
| | 319 | case 'getvariablestatobjects'
|
| | 320 | throwError = true;
|
| | 321 |
|
| | 322 | workspaceID = varargin{1};
|
| | 323 | theWorkspace = getWorkspace(workspaceID);
|
| | 324 |
|
| | 325 | values = varargin{2};
|
| | 326 | for i = 1:length(values)
|
| | 327 | variableName = values{i};
|
| | 328 | currentValue = evalin(theWorkspace, variableName);
|
| | 329 | values{i} = currentValue;
|
| | 330 | end
|
| | 331 | varargin = varargin(3:end);
|
| | 332 | varargout = {getStatObjectsJ(values, varargin{1}, varargin{2}, varargin{3})};
|
| | 333 |
|
| | 334 | case 'whosinformation'
|
| | 335 | throwError = false;
|
| | 336 | if varargin{1} == getDefaultWorkspaceID
|
| | 337 | error(message('MATLAB:codetools:workspacefunc:GettingVariablesfromDefaultWorkspaceNotImplemented'));
|
| | 338 | end
|
| | 339 | varargout = {getWhosInformation(evalin(getWorkspace(varargin{1}),'builtin(''whos'')'))};
|
| | 340 |
|
| | 341 | case 'readonly'
|
| | 342 | throwError = false;
|
| | 343 |
|
| | 344 | theWorkspace = getWorkspace(varargin{1});
|
| | 345 | variableName = varargin{2};
|
| | 346 | exists = [];
|
| | 347 |
|
| | 348 | %split and join are used to escape all quotes in the variable name
|
| | 349 | if ischar(theWorkspace)
|
| | 350 | exists = logical(evalin(theWorkspace, ['builtin(''exist'',''' strjoin(strsplit(variableName,''''),'''''') ''', ''var'')']));
|
| | 351 | elseif ismethod(theWorkspace, 'hasVariable')
|
| | 352 | try
|
| | 353 | exists = hasVariable(theWorkspace, strjoin(strsplit(variableName,''''),''''''));
|
| | 354 | catch
|
| | 355 | end
|
| | 356 | end
|
| | 357 | if isempty(exists)
|
| | 358 | exists = logical(evalin(theWorkspace, ['exist(''' strjoin(strsplit(variableName,''''),'''''') ''' , ''var'')']));
|
| | 359 | end
|
| | 360 | if ~exists
|
| | 361 | varargout{1} = false;
|
| | 362 | return
|
| | 363 | end
|
| | 364 |
|
| | 365 | varargin = varargin(2:end);
|
| | 366 | names = varargin{2};
|
| | 367 | values = varargin{2};
|
| | 368 | for i = 1:length(values)
|
| | 369 | variableName = values{i};
|
| | 370 | if ismethod(theWorkspace, 'getVariable')
|
| | 371 | try
|
| | 372 | currentValue = getVariable(theWorkspace, variableName);
|
| | 373 | catch
|
| | 374 | % In case of failure, fallback to using evalin
|
| | 375 | currentValue = evalin(theWorkspace, variableName);
|
| | 376 | end
|
| | 377 | else
|
| | 378 | try
|
| | 379 | currentValue = evalin(theWorkspace, variableName);
|
| | 380 | catch
|
| | 381 | varargout{1} = false;
|
| | 382 | return
|
| | 383 | end
|
| | 384 | end
|
| | 385 | values{i} = currentValue;
|
| | 386 | end
|
| | 387 | varargout = {areAnyVariablesReadOnly(values, names)};
|
| | 388 |
|
| | 389 | case 'rmfield'
|
| | 390 | throwError = false;
|
| | 391 |
|
| | 392 | % cannot use assignin because variable name can itself
|
| | 393 | % be a struct field
|
| | 394 | expr = [variableName ' = rmfield(' variableName ','''...
|
| | 395 | varargin{2} ''');'];
|
| | 396 | evalin(theWorkspace, expr);
|
| | 397 | requireUpdate = true;
|
| | 398 |
|
| | 399 | case 'renamefield'
|
| | 400 | throwError = false;
|
| | 401 | % cannot use assignin because variable name can itself
|
| | 402 | % be a struct field
|
| | 403 | expr = [variableName ' = renameStructField(' variableName ','''...
|
| | 404 | varargin{2} ''',''' varargin{3} ''');'];
|
| | 405 | evalin(theWorkspace, expr);
|
| | 406 | requireUpdate = true;
|
| | 407 |
|
| | 408 | case 'createUniqueField'
|
| | 409 | % Create new field in scalar struct, struct array, or nested
|
| | 410 | % structs.
|
| | 411 | throwError = false;
|
| | 412 |
|
| | 413 | %
|
| | 414 | % Uniquely named field (unnamed#) is added at the end, and assigned
|
| | 415 | % a value of 0.
|
| | 416 | %
|
| | 417 | fields = evalin(theWorkspace,['fieldnames(' variableName ')']);
|
| | 418 | fieldLength = length(fields) + 1;
|
| | 419 | newName = getNewName(fields);
|
| | 420 |
|
| | 421 | %
|
| | 422 | % determine the new order of the fields, if a 'insert before' field
|
| | 423 | % isn't specified, insert it at the beginning
|
| | 424 | %
|
| | 425 | if (numel(varargin) == 2)
|
| | 426 | insertBefore = varargin{2};
|
| | 427 | index = find(strcmp(insertBefore, fields));
|
| | 428 | else
|
| | 429 | index = [];
|
| | 430 | end
|
| | 431 |
|
| | 432 | %
|
| | 433 | % create the strings that will define the new field order - note
|
| | 434 | % that arrays aren't used so as to minimize the length of the input
|
| | 435 | % string to evalin below
|
| | 436 | %
|
| | 437 | if (~isempty(index))
|
| | 438 | % fieldOrder = [1:index-1 fieldLength index:fieldLength-1];
|
| | 439 | fieldOrder = sprintf('[1:%d %d %d:%d]', index-1, fieldLength, index, fieldLength-1);
|
| | 440 | else
|
| | 441 | % fieldOrder = [1:fieldLength];
|
| | 442 | fieldOrder = sprintf('[1:%d]', fieldLength);
|
| | 443 | end
|
| | 444 |
|
| | 445 | % first add a field
|
| | 446 | evalin(theWorkspace,sprintf('[%s.(''%s'')]=deal(0);', variableName, newName));
|
| | 447 |
|
| | 448 | % now reorder them (get rid of any indexing at the end as it is illegal
|
| | 449 | % to modify an element of a struct array with disimilarly structured
|
| | 450 | % elements
|
| | 451 | variableName = regexprep(variableName,'\(.+\)$','');
|
| | 452 | evalin(theWorkspace,sprintf('%s=orderfields(%s,%s);', variableName, variableName, fieldOrder));
|
| | 453 |
|
| | 454 | requireUpdate = true;
|
| | 455 |
|
| | 456 | case 'duplicateField'
|
| | 457 | throwError = false;
|
| | 458 |
|
| | 459 | fields = fieldnames(currentValue);
|
| | 460 | newName = getCopyName(varargin{2}, fields);
|
| | 461 | % cannot use assignin because variable name can itself
|
| | 462 | % be a struct field
|
| | 463 | expr = [variableName '.' newName ' = ' variableName '.' ...
|
| | 464 | varargin{2} ';'];
|
| | 465 | evalin(theWorkspace, expr);
|
| | 466 | requireUpdate = true;
|
| | 467 |
|
| | 468 | case 'createVariable'
|
| | 469 | throwError = false;
|
| | 470 |
|
| | 471 | expression = varargin{2};
|
| | 472 | newValue = evalin(theWorkspace,expression);
|
| | 473 | whoOutput = evalin(theWorkspace,'who');
|
| | 474 | newName = getNewName(whoOutput);
|
| | 475 | assignin(theWorkspace, newName, newValue)
|
| | 476 | requireUpdate = true;
|
| | 477 |
|
| | 478 | case 'assignVariable'
|
| | 479 | throwError = false;
|
| | 480 |
|
| | 481 | returnError = true;
|
| | 482 | expression = varargin{2};
|
| | 483 | newExpression = evalin(theWorkspace,expression);
|
| | 484 | newValue = setVariableValue(baseVariableName, baseValue, variableName, newExpression);
|
| | 485 | assignin(theWorkspace, baseVariableName, newValue)
|
| | 486 | requireUpdate = true;
|
| | 487 |
|
| | 488 | case 'copyworkspacevariables'
|
| | 489 | % workspacefunc('copyworkspacevariables', <cell array of variable names>, <cell array of variable identifiers>)
|
| | 490 | % Variable Names - Used to store and retrieve the variables from storage.
|
| | 491 | % Variable Identifiers - Used to query the values of variables from the caller workspace.
|
| | 492 |
|
| | 493 | % Set required variables
|
| | 494 | varargout = {};
|
| | 495 |
|
| | 496 | if isempty(varargin{1}) || isempty(varargin{2})
|
| | 497 | return
|
| | 498 | end
|
| | 499 |
|
| | 500 | % Set variable names and identifiers from varargin
|
| | 501 | varNames = varargin{1}; % cell array of variable names
|
| | 502 | varIdentifiers = varargin{2}; % cell array of variable identifiers
|
| | 503 |
|
| | 504 | % Set variable names in storage
|
| | 505 | copyPasteStorage('-setVarNames',varNames);
|
| | 506 |
|
| | 507 | % Get variable values from caller workspace and pass into storage
|
| | 508 | evalin('caller',[mfilename,...
|
| | 509 | '(''setcopyvarvalues'',{',cell2csv(varIdentifiers),'});']);
|
| | 510 |
|
| | 511 | case 'pasteworkspacevariables'
|
| | 512 | % workspacefunc('pasteworkspacevariables', <cell array of original variable names>, <cell array of new variable names>)
|
| | 513 | % Original Names - Used to query variables in storage
|
| | 514 | % New Names - Used to name variables in Caller workspace
|
| | 515 |
|
| | 516 | % Set required variables
|
| | 517 | varargout = {};
|
| | 518 |
|
| | 519 | if isempty(varargin{1}) || isempty(varargin{2})
|
| | 520 | return
|
| | 521 | end
|
| | 522 |
|
| | 523 | % Get cell array of stored and new variable names
|
| | 524 | origNames = varargin{1}; % cell array of original variable names
|
| | 525 | newNames = varargin{2}; % cell array of new variable names
|
| | 526 |
|
| | 527 | % Clean up any leaked variables
|
| | 528 | copyPasteStorage('-clearVariables',origNames);
|
| | 529 |
|
| | 530 | % Check to be sure that 'copy' has been called previously
|
| | 531 | if copyPasteStorage('-isempty')
|
| | 532 | return;
|
| | 533 | end
|
| | 534 |
|
| | 535 | % Create variables in caller workspace
|
| | 536 | for varNameIdx = 1:numel(newNames)
|
| | 537 | evalin('caller',[newNames{varNameIdx},...
|
| | 538 | ' = ',mfilename,'(''getcopyvarvalues'',''',...
|
| | 539 | origNames{varNameIdx},''');']);
|
| | 540 | end
|
| | 541 |
|
| | 542 | requireUpdate = true;
|
| | 543 |
|
| | 544 | case 'clearstoredworkspacevariables'
|
| | 545 | % workspacefunc('clearstoredworkspacevariables', <cell array of variables to be saved>{optional})
|
| | 546 | % Saved Variables - Optional way of preventing certain variables from being cleared from storage.
|
| | 547 |
|
| | 548 | % Set required variables
|
| | 549 | varargout = {};
|
| | 550 |
|
| | 551 | % Clean up variable storage
|
| | 552 | if isempty(varargin)
|
| | 553 | copyPasteStorage('-clearAllVariables');
|
| | 554 | else
|
| | 555 | copyPasteStorage('-clearVariables',varargin{1});
|
| | 556 | end
|
| | 557 |
|
| | 558 | case 'getcopyvarnames'
|
| | 559 | % Return names of all variables in storage (Used for testing)
|
| | 560 | varargout = {copyPasteStorage('-getVarNames')};
|
| | 561 |
|
| | 562 | % The following 2 cases are called from previous cases and require data
|
| | 563 | % to be input from the caller workspace
|
| | 564 | case 'setcopyvarvalues'
|
| | 565 | % Set required variables
|
| | 566 | varargout = {};
|
| | 567 |
|
| | 568 | % Set variable values in storage
|
| | 569 | copyPasteStorage('-setVarValues',varargin{1});
|
| | 570 |
|
| | 571 | case 'getcopyvarvalues'
|
| | 572 | % Return variable values from storage
|
| | 573 | varargout = {copyPasteStorage('-getVarValues',varargin{1})};
|
| | 574 |
|
| | 575 | case 'clearstoredworkspace'
|
| | 576 | getWorkspaceID('-clear',varargin{1})
|
| | 577 |
|
| | 578 | case 'createComplexVector'
|
| | 579 | varargout = {createComplexVector(varargin{:})};
|
| | 580 |
|
| | 581 | otherwise
|
| | 582 | error(message('MATLAB:workspacefunc:unknownOption'));
|
| | 583 | end
|
| | 584 |
|
| | 585 | if requireUpdate
|
| | 586 | reportWSChange
|
| | 587 | end
|
| | 588 |
|
| | 589 | catch e
|
| | 590 | if returnError
|
| | 591 | varargout{1} = e.message;
|
| | 592 | elseif throwError
|
| | 593 | rethrow(e)
|
| | 594 | else
|
| | 595 | showError(whichcall, e)
|
| | 596 | varargout{1}=[]; % to suppress hard error message display on command window
|
| | 597 | end
|
| | 598 | end
|
Other subfunctions in this file are not included in this listing.