- Undocumented Matlab - https://undocumentedmatlab.com -

Removing user preferences from deployed apps

Posted By Yair Altman On November 7, 2012 | No Comments

Every now and then I stumble upon a MathWorks technical note/solution [1] that uses undocumented features. I already posted a few of these here (e.g., this one [2] for the continuous slider movement [3] issue, and this one [4] for the figure maximization/minimization [5] issue). In fact, you may be surprised to know that there are dozens of other official technical notes, solutions and articles that use undocumented/unsupported Matlab functionality. Yummy!
Of the numerous official resources containing unofficial information (I can’t help but smile), do you have a personal favorite? If so, please post a link in a comment [6] below, with a short description of what it is about.
Today, I show one such technical solution (http://www.mathworks.com/support/solutions/en/data/1-1UOGW7/ [7]) that solves a privacy issue in deployed (compiled) Matlab applications. IMHO, the privacy issue is generally harmless, but users can judge by themselves. Obviously, if someone is stupid enough to store real secret information in the Matlab preferences, then the privacy issue could become more important.

The solution refers to Matlab release 7.1 (R14SP3), but is also relevant for later releases. I will let the text speak for itself:

When I execute an application compiled using MATLAB Compiler 4.3 (R14SP3), how can I prevent personal information such as my MATLAB preferences information from being extracted?

Problem Description:

I have compiled an application ‘test’ using MATLAB Compiler 4.3 (R14SP3). Upon execution I noticed that a new folder gets created in the ‘test_mcr’ folder. This folder is named ‘test_‘.
Opening this folder revealed that some of the files were stored in my MATLAB preferences directory, including my MATLAB preferences file.
I want to know if there is any way to avoid the creation of this directory when my application executes.

Solution:

It is not possible to avoid having MATLAB Compiler include the preferences directory in each compiled application. The Compiler includes the user’s preference information because the application may rely on it. There are two possible workarounds:
1) On UNIX, use SETENV and the ! operator to compile. For example:

!setenv MATLAB_PREFDIR /tmp/emptydir;
mcc -Nmgv test

On Windows, open a DOS command window and execute

set -- USERPROFILE = C:\Temp\emptydir

Start MATLAB from this DOS prompt (to use the appropriate environment variable in the MATLAB session) and execute the MCC command at the MATLAB command prompt.
Note: If you are using MATLAB Compiler 4.4 (R2006a) you can also use the SETENV function at the MATLAB command prompt

setenv('-- USERPROFILE','C:\Temp\emptydir');
mcc -Nmgv test

This temporarily sets the preference directory to /tmp/emptydir or C:\Temp\emptydir, and the compiled application will therefore only have the default settings (or the preference directory settings from your installation of MATLAB 7.0.4 (R14SP2)).
2) Move the contents of the preference directory, or rename the directory, before compilation, and restore afterwards. To do this on Windows, for example, execute the following command to compile the function test.m:

mymcc -m test.m

The code of the function mymcc is:

function mymcc(varargin)
% MYMCC does the same as MCC. The only difference is that the created
% application doesn't contain information of the MATLAB
% prefdir anymore.
%
% For the description of possible options please look at the help of
% the command MCC.
command = 'mcc ';
for i = 1 : length(varargin)
   command = [command,varargin{i},' '];
end
a = prefdir;
b = regexp(a,'\','start');
c = a(b(end)+1:end);
p = cd;
cd(prefdir)
cd ..
!md tmp
cd(prefdir)
!move * ../tmp
cd(p)
% Because of the displacement MATLAB cannot access the saved preferences
% and that's why you have to indicate each time, which compiler shall be used.
try
   eval(command);
catch
   warning('WarnTests:Warning', ...
           'Calling mcc causes Error. Please check your input arguments.\n No file was compiled ...\n')
end
cd(prefdir)
cd ..
cd tmp
eval(['!move * ../',c]);
cd ..
!rmdir tmp
cd(p);

A similar function could also be written for the UNIX platform.
Note that the above workaround is not officially supported by MathWorks.

Why are my preference files included in my stand-alone application created with MATLAB Compiler 4.9 (R2008b)? [8]
Problem Description:
If I look in the cache folder that my compiled application creates, I can see that the .matlab folder contains my preference files with my last development activities in plain text. The files are: cwdhistory.m, history.m, matlab.prf, MATLAB_Editor_State.xml, etc. How can I avoid the inclusion of these files in the stand-alone application?
Solution:
The stand-alone does not include your preference files. You can see the preference files in the cache folder because you are running the stand-alone in your development environment. At runtime, the stand-alone determines those preferences from a local MATLAB installation and copies them to the cache folder for it’s own use. If you run your stand-alone application on a machine without a MATLAB installation, you will see that the cache folder does not contain your preference files.

Categories: Low risk of breaking in future versions, Undocumented feature


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/removing-user-preferences-from-deployed-apps

URLs in this post:

[1] technical note/solution: http://www.mathworks.com/support/product/technical-solutions-index.html

[2] this one: http://www.mathworks.com/support/solutions/en/data/1-3SR0YI/

[3] continuous slider movement: http://undocumentedmatlab.com/blog/continuous-slider-callback/

[4] this one: http://www.mathworks.com/support/solutions/en/data/1-3MY8PN/

[5] figure maximization/minimization: http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/

[6] comment: http://undocumentedmatlab.com/blog/removing-user-preferences-from-deployed-apps/#respond

[7] http://www.mathworks.com/support/solutions/en/data/1-1UOGW7/: http://www.mathworks.com/support/solutions/en/data/1-1UOGW7/

[8] Why are my preference files included in my stand-alone application created with MATLAB Compiler 4.9 (R2008b)?: http://www.mathworks.com/support/solutions/en/data/1-9BKF3F/

[9] Using pure Java GUI in deployed Matlab apps : https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps

[10] Changing system preferences programmatically : https://undocumentedmatlab.com/articles/changing-system-preferences-programmatically

[11] Splash window for deployed applications : https://undocumentedmatlab.com/articles/splash-window-for-deployed-applications

[12] Speeding up compiled apps startup : https://undocumentedmatlab.com/articles/speeding-up-compiled-apps-startup

[13] uicontrol side-effect: removing figure toolbar : https://undocumentedmatlab.com/articles/uicontrol-side-effect-removing-figure-toolbar

[14] User-defined tab completions – take 2 : https://undocumentedmatlab.com/articles/user-defined-tab-completions-take-2

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.