Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worksession Variables - MATLAB

Tags:

matlab

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?

like image 254
Tom Wainwright Avatar asked Jun 03 '26 08:06

Tom Wainwright


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!