Matlab Associate Files 自动关联代码文件

本文提供代码,用于解决M文件无法直接用Matlab打开的问题。通过生成并运行associateFiles.reg注册表文件,可以恢复M文件与Matlab的正确关联。
摘要由CSDN通过智能技术生成

Matlab Associate Files 自动关联代码文件

文件说明

以下代码用于将m文件自动关联到Matlab软件,从而保证直接开启编辑器。有时候程序会自动抽风,留存备用。
运行该文件,将会产生一个associateFiles.reg注册表文件,运行,即可解决问题。

function associateFiles(action, userExtList, fileStr)

% associateFiles(action, extList, fileStr)
%
% Makes a registry files that can be used to set correct file associantions on
% a windows platform. The following MATLAB file extensions are supported:
% .m, .mat, .fig, .mexw32, .mexw64, .p, .mdl, .mdlp, .slx, .mldatx, .req,
% .sldd, .slddc, .slxp, .sltx, .mn, .mu, .muphlp, .xvc, .xvz, .ssc, .mlapp,
% .mlappinstall, .mltbx, .mlpkginstall, .mlprj
%
% INPUT:
% action  - optional string. 
%           * 'add' (default) adds/rewrites the MATLAB file association registry
%              keys for this version.
%           * 'delete' deletes the MATLAB file association registry entries for
%              ALL versions of MATLAB (including "old style" ones)
%           * 'deleteadd' is the same as 'delete' followed by 'add'  
% extList - optional string or cell array of strings containing the file
%           extensions that should be associated with this version. Default is
%           all MATLAB file extension (see above).
% fileStr - optional string with the name of the registry file to be written 
%           (possibly including path). Default is the file
%           'MatlabFileAssocFix.reg' in the current directory.
%
% USAGE:
% 1) Run with desired options (see above). A registry file should have been 
%    created. 
% 2) Exit all running instances of MATLAB.
% 3) Make a backup copy of the windows registry if you need to restore the 
%    changes, see https://support.microsoft.com/en-us/kb/322756
% 4) Double click on the created file (possibly need to enter a password) and
%    confirm.
% 5) Restart Windows (or explorer.exe).
% 6) The MATLAB files should now be associated with the MATLAB version that the
%    registry file was created in and e.g. m-files should be opened in an
%    already running instance of MATLAB.
%
% EXAMPLES:
% * associateFiles('deleteadd') - Makes a registry files that deletes all
%   previous MATLAB file association registry keys and write new ones that
%   associates all MATLAB files with the MATLAB version that the registry file
%   was created in.
% * associateFiles('', {
   '.m', '.mat', '.fig'}, 'myFile') - Makes a registry file
%   "myFile.reg" that associates m-, mat- and fig-files with the MATLAB version
%   that the registry file was created in. 
%
% VERSION 1.0

% 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')
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值