Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to keep workspace variable values in memory?
    primarykey
    data
    text
    <p>I have a workspace with a set of variables (e.g. <code>var1</code>, <code>var2</code>, <code>var3</code>, <code>var4</code>, <code>var5</code>, <code>var6</code> and <code>var7</code>). There is a .m function (using <code>assignin('base',...)</code> containing the defaults values for <code>var1</code> to <code>var7</code>. The user will open Matlab and run this function in order to have the variable and their default values added to the workspace. The user might after that change the values of some variables or even delete some variables. </p> <p>Now, if the user has changed variable values and that he run the function again, i don't want the default value to overwrite the new value entered by the user.</p> <p>Here what i have so far :</p> <pre><code>S = evalin('base','whos'); listWorkspaceVar = {S.name}; compareCellArrayList = ismember(listWorkspaceVar, listParam); % logical vector, listParam is the list of variables in the function assigning the default values, so here listParam would be = {'var1' 'var2' 'var3' 'var4' 'var5' 'var6' 'var7'}) if any(compareCellArrayList) % check if there is at least one declared variable in the base workspace tempWorkspaceVarVal = cell(sum(compareCellArrayList),2); % Pre-allocating, 1 column for the variable name and 1 column for the variable value jj = 1; for ii = 1:size(S,1) if compareCellArrayList(1, ii) tempWorkspaceVarVal(jj,:) = {listWorkspaceVar{1, ii}, ... evalin('base',listWorkspaceVar{1, ii})}; jj = jj + 1; end end theDefaultValueFunction() % The function that assignin the default values cellfun(@(x,y) assignin('base', x, y), ... tempWorkspaceVarVal(:,1), tempWorkspaceVarVal(:,2)) % Overwrite with the temp values in order to keep the new entered values end </code></pre> <p>If I don't want to change the function assignin the default values, is my method the best one to keep the new modified variable values? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload