2
votes

I'm writing a series of MATLAB functions which communicate with a server through urlread. Each function in this package that makes this call requires an authentication username and key.

I would rather not require the user to pass in the username and key when calling every function. Instead, I prefer to have a signin(username, key) function that sets/saves these variables in such a way that each function in this package can recall.

My solution right now is for signin.m to save username and key to a temporary file and to modify finish.m to erase this temporary file when MATLAB closes. Each function in the package would load these variables from that temporary file. However, if someone force quits MATLAB, this temporary file will not be erased (right?).

Another solution was to have signin save username and key as global variables. However, if a user calls clear all, these variables will be removed the workspace and the user will need to call signin again (which is a nuisance).

Is there some way to set 'session' variables that are global and are not removed with a clear all command? Any other suggestions?

3

3 Answers

4
votes

You're almost there. A few useful features:

persistent

First, you should be using persistent variables rather than global variables. They are like globals, but scoped to a single function. They're just better.

mlock

Run mlock within a function to prevent the clear or clear all command from clearing data associated with that function.


So, for example, you could define a quick function to help a username and key within a Matlab sessionb as follows:

function [name, key] = credentials(varargin)
persistent USERNAME KEY
if nargin==3 && ischar(varargin{1}) && strcmpi(varargin{1},'set')
    USERNAME = varargin{2};
    KEY= varargin{3};
    mlock;
else
    name = USERNAME;
    key = KEY;
end

Then you could use it like this:

%First, set the credentials
credentials set SOMEUSERNAME SOMEKEY

% ....  do some work ....

clear   %As part of your work, clear all variables

% ....  do some more work ....


%Get the credentials later
[name, key] = credentials;
5
votes

You might consider the preferences feature of MATLAB. It works with the functions setpref, addpref, rmpref, and getpref. I use these tools in a few applications, and they work nicely.

One minor problem is if you will be calling these tools frequently. Since getpref uses a read from a disk file to access the prefs, it is not as fast as it might be. So IF you must have the absolute maximum speed due to frequent calls, then a mixture of persistent variables seems to work well for me. Thus, I have a function that I use to access the preference in question. It contains the pref in a persistent variable. If this is the first time the pref is queried, then that persistent variable will be empty, so I do a getpref call to fill it. (This is a nice feature, since the pref will persist across MATLAB sessions.) When you need to change the variable, do a setpref too.

0
votes

Another alternative is the official startup.m file and the related Startup Folder. These could be used to do pretty much anything, including the other solutions provided.