MATLAB Answers

Storing persistent variable in a standalone GUI

8 views (last 30 days)
Jacob Mohin
Jacob Mohin on 27 Sep 2012
Hello All,
I have a GUI standalone app I've written that works beutifully in MATLAB, uncompiled. It stores several settings/user preferences in an XML file and reads them when appropriate.
The problem is, when I compile the program to use as a standalone, even using relative paths, the xml file never gets updated between uses of the application. Is there a better way to store variable persistently inbetween different instances of my application?

  0 Comments

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 27 Sep 2012
Edited: Image Analyst on 27 Sep 2012
Use save() to store them in a proprietary MATLAB format file. Then use load() to retrieve them. That's what I do.
Is there any particular reason why you wanted or needed it in an XML file? Sure it's a text file, but it's not particularly easy to read or edit. I wouldn't use XML unless you needed to communicate with some other program for some reason (one that doesn't understand MATLAB's mat file format).
Also, you have to be careful where you store it. If you're trying to store it somewhere under the Program Files folder, Windows 7 won't let you. You can store files in certain places in the c:\User folder and anywhere under the c:\ProgramData folder. The ProgramData folder is a common place that Win7 apps store their initialization settings. Take a look inside yours.

  0 Comments

Sign in to comment.


Robert Cumming
Robert Cumming on 27 Sep 2012
There is no reason why you shouldn't be able to update your settings in the XML file when its compiled - can you capture an error? Go old school and put lots of disp statements in your code to see where it gets to when the settings are being updated - that way you can maybe see where an error is - and hence be able to fix it.
You can launch your code from a CMD to make sure you can see any messages.
I seconds IA comment of "why XML"?
I use a human readable txt file for this purpose - why text and not standard save() - I do this so that I can quickly customise it on the fly when developing - if and when required.

  0 Comments

Sign in to comment.


Romesh
Romesh on 3 Oct 2012
There is a likely reason that you have this problem. When the program is compiled, you might have included the XML file inside the archive. Then when you load it, it would be using the static compiled version. The solution is to store the XML file completely independently of your program. For example, store it in a Windows application data folder (use something like %APPDATA%\yourprogram) or in a user directory (e.g. ~/.yourprogram/) - IIRC it is difficult to identify which folder the exe/binary is being run from, hence the use of these absolute paths.

  1 Comment

Image Analyst
Image Analyst on 3 Oct 2012
That is true. If you just stick stuff in the folder where your exe lives, it won't find it if you use relative paths. That's because what you think is the exe is really a self-extracting archive and the real exe that gets run is in some special secret folder where no one would ever think to look. Just print out ctfroot in the startup code of the app to see where the "real" exe lives.

Sign in to comment.

Sign in to answer this question.