问题
Condition: unsuccessful userpath
with addpath
and savepath
Differential conditions: unable to start Matlab as sudo/root here
Support: MATLAB is not intended to be run by the super user - Service desk.
Settings
echo "export MATLAB_USE_USERWORK=1" >> $HOME/.bashrc
matlab -nodesktop -nosplash -r \
"userpath('/home/masi/Documents/bin/matlab/'); exit;"
Situations which cause userpath
to be empty (''
)
Test code 1
userpath('/home/masi/Documents/bin/matlab/') home='/home/masi/'; savepath(fullfile(home, 'pathdef.m')); % ~/pathdef.m originally but reject because expansion of ~ not working userpath
Output
''
Expected output
'/home/masi/Documents/bin/matlab/:'
Complete reiteration from Matlab's prompt after starting Matlab
>> userpath ans = /home/masi/Documents/bin/matlab/: >> home='/home/masi/'; >> savepath(fullfile(home, 'pathdef.m')); >> userpath ans = ''
Test code 2
if (userpath == '') userpath('/home/masi/Documents/bin/matlab/') addpath('/home/masi/Documents/Math/') savepath '/home/masi/pathdef.m' end % Visualising path in Home > Environment > Set Path
Output:
/home/masi/Documents/Math/
stays in path, but/home/masi/Documents/bin/matlab/
not. Expected output: both stay in path.Test code 3
% Edit the Matlab preference file as root. [Kusalananda] % My startup.m userpath('/home/masi/Documents/bin/matlab/') addpath('/home/masi/Documents/Math/') savepath '/home/masi/startup.m' % Visualising path in Home > Environment > Set Path
Output:
/home/masi/Documents/Math/
stays in path, but/home/masi/Documents/bin/matlab/
not. Expected output: both stay in path.Test code 4
% Use MATLABPATH environment variable instead. [Kusalananda]
How?
Matlab: 2016a
System: Linux Ubuntu 16.04 64 bit
Hardware: Macbook Air 2013-mid
Related: setup wfdb in Linux here where the official script is incomplete
回答1:
I could not maintain stably the userpath
in the system so a solution which works in the beginning of the functions
checkSystemMemory();
%% TODO Virtual memory; enable virtual memory on HDDs.
% http://askubuntu.com/q/799834/25388
home=char(java.lang.System.getProperty('user.home')); % all systems
if (isempty(userpath))
userpath(fullfile(home, 'Documents/bin/matlab/') )
% list software locations in the project location
addpath(fullfile(home, 'Documents/Math/') )
% list software locations in the centralised location
addpath(fullfile(home, 'Documents/Programming/Matlab/export_fig-master/') )
% http://stackoverflow.com/a/38188945/54964
if ( not( com.mathworks.services.Prefs.getStringPref('CurrentKeyBindingSet') == 'Windows' ) )
com.mathworks.services.Prefs.setStringPref('CurrentKeyBindingSet', 'WindowsDefaultSet.xml')
end
% Event errors else with touchpad scroll
!synclient HorizTwoFingerScroll=0
%savepath '/home/masi/pathdef.m'
savepath(fullfile(home, 'startup.m'));
end
function checkSystemMemory()
%% Java for rdsamp - restart for the change to take effect if necessary.
% https://physionet.org/physiotools/matlab/wfdb-app-matlab/faq.shtml
% http://se.mathworks.com/matlabcentral/answers/92813-how-do-i-increase-the-heap-space-for-the-java-vm-in-matlab-6-0-r12-and-later-versions
% http://stackoverflow.com/a/35971040/54964
isSetHeapSizeMemoryMax = com.mathworks.services.Prefs.getIntegerPref('JavaMemHeapMax'); % MB
if (isSetHeapSizeMemoryMax < 2000)
com.mathworks.services.Prefs.setIntegerPref('JavaMemHeapMax', 2048); % MB
fprintf('Quitting Matlab Function because memory %d is insufficient.', ...
isSetHeapSizeMemoryMax);
return
end
assert(java.lang.Runtime.getRuntime.maxMemory/1e9 >= 1.9, 'Java heap size too small');
end
来源:https://stackoverflow.com/questions/38283814/why-savepath-resets-userpath-in-matlab