Documentos de Académico
Documentos de Profesional
Documentos de Cultura
M%%%
%% This is the part which works with most of MATLAB File associations
associateFiles('add',{'.m', '.mat', '.fig', '.p', '.mexw64', '.mlapp', '.mltbx',
'.mldatx', '.req', '.mlappinstall', '.mlpkginstall', '.mdlp', '.slxp', '.mdl',
'.slx', '.sltx', '.ssc', '.mn', '.mu', '.muphlp', '.xvc', '.xvz', '.sldd',
'.slddc',['.' mexext]});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%function%%%%%%%%%%%% ("the file")
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Defualt input
if (nargin < 1 || isempty(action))
action = 'add';
end
if (nargin < 2)
userExtList = {};
end
if (nargin < 3)
fileStr = '';
end
if (~iscell(userExtList))
if (isempty(userExtList))
userExtList = {};
else
userExtList = {userExtList};
end
end
% Sanity check
if (~ischar(action) || (~strcmpi(action, 'add') && ...
~strcmpi(action, 'delete') && ~strcmpi(action, 'deleteadd')))
error('The action to perform must be ''add'', ''delete'' or ''deleteadd''!')
end
if (~isempty(userExtList) && ~min(cellfun(@ischar, userExtList)))
error('The file extension list must be a string or a cell array of strings!')
end
if (~ischar(fileStr))
error('The file to write to must be a string!')
end
% Get the currently running MATLAB version
verStr = regexp(version, '(\d*?\.\d*?\.\d*?)\.', 'tokens');
verStr = verStr{1}{1};
verNum = str2double(regexprep(verStr, '(\d*?\.\d*)[\x0000-\xffff]*', '$1'));
verHex = sprintf('%04x', str2double(regexprep(verStr, ...
'(\d*?)\.[\x0000-\xffff]*', '$1')), str2double(regexprep(verStr, ...
'\d*?\.(\d*?)\.[\x0000-\xffff]*', '$1')));
% Get 32/64-bit
arch = computer;
switch arch
case 'PCWIN'
binFolder = 'win32';
case 'PCWIN64'
binFolder = 'win64';
end
binPath = fullfile(matlabroot, 'bin', binFolder);
% Explorer keys
[status, result] = dos(['reg query ', explorerKey, ' /f .', fileExt, ...
' /k /e']);
if (~status)
keys = regexp(result, '(HKEY_CURRENT_USER[\x0000-\xffff]*?)\n', 'tokens');
rmKeys = [rmKeys, keys{:}];
end
% Write to file
if (~isempty(rmKeys))
fprintf(fid, '%s\r\n\r\n', [';REMOVES ', upper(fileExt), ...
' FILE ASSOCIATIONS']);
for keyNo = 1 : length(rmKeys)
key = rmKeys{keyNo};
fprintf(fid, '%s\r\n\r\n', ['[-', key, ']']);
end
end
end
% ADD KEYS
if (~strcmpi(action, 'delete'))
% Get text Persistent Handler
[status, result] = dos(...
'reg query HKEY_CLASSES_ROOT\.txt\PersistentHandler /ve');
if (~status)
PersistentHandler = regexp(result, '\{[\x0000-\xffff]*?\}', 'match');
PersistentHandler = PersistentHandler{1};
else
PersistentHandler = '';
end
% DDE call
ddeCall = 'ShellVerbs.Matlab';
if (verNum > 8)
% Changed from R2013a
ddeCall = [ddeCall, '.', verStr];
end
% Default icon
defIcon = 'm';
if (~exist(fullfile(binPath, 'm.ico'), 'file'))
defIcon = '';
end
% Path to MATLAB binary directory with \\
binPathStr = regexprep(binPath, '\\', '\\\\');
% DDE keys
ddeData = fileExtCell(ismember(fileExtCell(:, 1), fileExt), :);
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
']%r@="', ddeData{2}, '"%r'];
if (~isempty(ddeData{3}))
key = [key, '"FriendlyTypeName"="@', binPathStr, '\\matlab.exe', ...
',', ddeData{3}, '"%r'];
end
key = [key, '%r'];
% Icon
icon = fileExt;
if (~exist(fullfile(binPath, [icon, '.ico']), 'file'))
icon = defIcon;
end
if (~isempty(icon))
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\DefaultIcon]%r@="', binPathStr, '\\', icon, '.ico,0"%r%r'];
end
% Shell actions
for shellActionNo = 4:5
ddePar = ddeData{shellActionNo};
if (~isempty(ddePar))
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\Shell\', ddePar{1}, ']%r@="', ddePar{1}, '"%r%r'];
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\Shell\', ddePar{1}, '\command]%r@="\"', binPathStr, ...
'\\matlab.exe\""%r%r'];
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\Shell\', ddePar{1}, '\ddeexec]%r@="', ddePar{2}, '"%r%r'];
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\Shell\', ddePar{1},'\ddeexec\application]%r@="', ...
ddeCall, '"%r%r'];
key = [key, '[HKEY_CLASSES_ROOT\MATLAB.', fileExt, '.' verStr, ...
'\Shell\', ddePar{1},'\ddeexec\topic]%r@="system"%r%r'];
end
end
% Explorer keys
key = [key, '[', explorerKey, '\.', fileExt, '\OpenWithProgids]%r'];
if (strcmpi(fileExt, 'm'))
key = [key, '"m_auto_file"=hex(0):%r'];
end
key = [key, '"MATLAB.', fileExt, '.', verStr, '"=hex(0):%r%r'];
if (~isempty(ddeData{4}))
% Add key
key = [key, '[', explorerKey, '\.', fileExt, ...
'\OpenWithList]%r"a"="MATLAB.exe"%r"MRUList"="a"%r%r'];
else
key = [key, '[', explorerKey, '\.', fileExt, '\OpenWithList]%r%r'];
end
% Write to file
fprintf(fid, '%s\r\n\r\n', [';ADD ', upper(fileExt), ...
' FILE ASSOCIATIONS']);
lines = regexp(key, '([\x0000-\xffff]*?)%r', 'tokens');
for lineNo = 1 : length(lines)
fprintf(fid, '%s\r\n', lines{lineNo}{1});
end
end
end
% Cloese file
fclose(fid);