解决MATLAB不能设置为.m文件默认打开方式

2023-05-16

转载:https://blog.csdn.net/yujiaerzong/article/details/77624167

1. 将下面代码复制保存为 associateFiles.m 文件。

2. 在MATLAB中运行 associateFiles.m 文件,即在命令行中输入 associateFiles 然后 回车。

3. 步骤2程序运行生成文件 MatlabFileAssocFix.reg 。

4. 关闭MATLAB,双击运行 MatlabFileAssocFix.reg 。

5. 设置.m文件默认打开程序为MATLAB2016a 完工。

 

 

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('x', 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);
 
 
% Known MATLAB files with possible DDE actions
fileExtCell = {...
  'fig' ,   'MATLAB Figure'              , '-62'                       , ...
  {'Open', 'uiopen(''%1'',1)'}           , []                          ; ...
  'm'     , 'MATLAB Code'                , '-58'                       , ...
  {'Open', 'uiopen(''%1'',1)'}           , {'Run', 'run(''%1'')'}      ; ...
  'mat'   , 'MATLAB Data'                , '-59'                       , ...
  {'Load', 'load(''%1'')'    }           , {'Open', 'uiimport(''%1'')'}; ...
  'mdl'   , 'Simulink Model'             , '-61'                       , ...
  {'Load', 'uiopen(''%1'',1)'}           , []                          ; ...
  'mdlp'  , 'Simulink Protected Model'   , '-72'                       , ...
  []                                     , []                          ; ...
  'mexw32', 'MATLAB MEX'                 , '-63'                       , ...
  []                                     , []                          ; ...
  'mexw64', 'MATLAB MEX'                 , '-63'                       , ...
  []                                     , []                          ; ...
  'mn'    , 'MuPAD Notebook'             , '-66'                       , ...
  {'Open', 'mupad(''%1'')'}              , []                          ; ...
  'mu'    , 'MuPAD Code'                 , '-67'                       , ...
  {'Open', 'uiopen(''%1'',1)'}           , []                          ; ...
  'muphlp', 'MuPAD Help'                 , '-68'                       , ...
  {'Open', 'doc(symengine, ''%1'')'}     , []                          ; ...
  'p'     , 'MATLAB P-code'              , '-60'                       , ...
  []                                     , []                          ; ...
  'slx'   , 'Simulink Model (SLX format)', '-73'                       , ...
  {'Open', 'uiopen(''%1'',1)'}           , []                          ; ...
  'ssc'   , 'Simscape Model'             , '-65'                       , ...
  {'Open', 'uiopen(''%1'',1)'}           , []                          ; ...
  'xvc'   , 'MuPAD Graphics'             , '-69'                       , ...
  {'Open', 'mupad(''%1'')'}              , []                          ; ...
  'xvz'   , 'MuPAD Graphics'             , '-70'                       , ...
  {'Open', 'mupad(''%1'')'}              , []                          ; ...
  'mlapp'       , 'MATLAB Application'              , [] , [], []      ; ... 
  'mltbx'       , 'MATLAB Toolbox'                  , [] , [], []      ; ... 
  'mldatx'      , 'Simulink Scenario'               , [] , [], []      ; ...  
  'req'         , 'Simulink Requirements Link'      , [] , [], []      ; ... 
  'sldd'        , 'Simulink Dictionary'             , [] , [], []      ; ... 
  'slddc'       , 'Simulink Dictionary'             , [] , [], []      ; ...      
  'mlappinstall', 'MATLAB Application'              , [] , [], []      ; ...  
  'mlpkginstall', 'MATLAB Support Package'          , [] , [], []      ; ... 
  'slxp'        , 'Simulink Protected Model Package', [] , [], []      ; ... 
  'sltx'        , 'Simulink Template'               , [] , [], []      ; ... 
  'mlprj'       , 'MATLAB Project'                  , [] , [], []};
 
% Possibly trim list
if (~isempty(userExtList))
  fileExtCell = fileExtCell(ismember(fileExtCell(:, 1), ...
    regexprep(userExtList, '\.', '')), :);
end
 
% Make registry file
if (~isempty(fileStr))
  % Possibly add file extension
  [~, ~, tmp] = fileparts(fileStr);
  if (isempty(tmp))
    fileStr = [fileStr, '.reg'];
  end
  fid = fopen(fileStr, 'w');
else
  fid = fopen('MatlabFileAssocFix.reg', 'w');
end
if (fid == -1)
  error('Failed to create registry file')
end
% Write intial lines
fprintf(fid, '%s\r\n\r\n', 'Windows Registry Editor Version 5.00');
fprintf(fid, '%s\r\n\r\n', ';FIXES MATLAB FILE ASSOCIATIONS');
 
 
% REMOVE OLD KEYS
explorerKey = ['HKEY_CURRENT_USER\Software\Microsoft\Windows\', ...
  'CurrentVersion\Explorer\FileExts'];
% Iterate over file extensions
for fileExtNo = 1 : size(fileExtCell, 1)
  rmKeys  = {};
  fileExt = fileExtCell{fileExtNo, 1};
  
  % File extension keys
  [status, result] = dos(['reg query HKEY_CLASSES_ROOT /f .', fileExt, ...
    ' /k /e']);
  if (~status)
    keys = regexp(result, '(HKEY_CLASSES_ROOT[\x0000-\xffff]*?)\n', 'tokens');
    rmKeys = [rmKeys, keys{:}];
  end
  
  % Old style keys without version numbers
  if (~strcmpi(fileExt, 'mexw64'))
    % Uses single DDE key for mex files
    if (strcmpi(fileExt, 'mexw32'))
      fileExtTmp = 'mex';
    else
      fileExtTmp = fileExt;
    end
    [status, result] = dos(['reg query HKEY_CLASSES_ROOT /f ', ...
      fileExtTmp, 'file /k /e']);
    if (~status)
      keys = regexp(result, '(HKEY_CLASSES_ROOT[\x0000-\xffff]*?)\n', ...
        'tokens');
      rmKeys = [rmKeys, keys{:}];
    end
  end
  
  % New style keys with version number
  if (strcmpi(action, 'add'))
    % Only remove keys related to this version
    [status, result] = dos(['reg query HKEY_CLASSES_ROOT /f MATLAB.', ...
      fileExt, '.', verStr ' /k']);
  else
    % Remove keys related to ALL version
    [status, result] = dos(['reg query HKEY_CLASSES_ROOT /f MATLAB.', ...
      fileExt, '. /k']);
  end
  if (~status)
    keys = regexp(result, '(HKEY_CLASSES_ROOT[\x0000-\xffff]*?)\n', 'tokens');
    rmKeys = [rmKeys, keys{:}];
  end
  
  % 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, '\\', '\\\\');
  
  % Write Shell Open key
  key = ['[HKEY_CLASSES_ROOT\Applications\MATLAB.exe\shell\open', ...
    '\command]%r', '@="\"', binPathStr, '\\MATLAB.exe\" \"%1\""%r%r'];
  fprintf(fid, '%s\r\n\r\n', ';ADD SHELL OPEN');
  lines = regexp(key, '([\x0000-\xffff]*?)%r', 'tokens');
  for lineNo = 1 : length(lines)
    fprintf(fid, '%s\r\n', lines{lineNo}{1});
  end
  
  % Iterate over file types
  for fileExtNo = 1 : size(fileExtCell, 1)
    fileExt = fileExtCell{fileExtNo, 1};
    
    % File extension keys
    key  = ['[HKEY_CLASSES_ROOT\.', fileExt, ']%r@="MATLAB.', fileExt, '.', ...
      verStr, '"%r'];
    if (strcmpi(fileExt, 'm') && ~isempty(PersistentHandler))
      % Add some values
      key = [key, '"Content Type"="text/plain"%r', ...
        '"PerceivedType"="Text"%r'];
    end
    key = [key, '%r'];
    key = [key, '[HKEY_CLASSES_ROOT\.', fileExt, ...
      '\OpenWithProgids]%r"MATLAB.', fileExt, '.', verStr, '"=""%r%r'];
    if (strcmpi(fileExt, 'm') && ~isempty(PersistentHandler))
      key = [key, '[HKEY_CLASSES_ROOT\.', fileExt, ...
        '\PersistentHandler]%r@="', PersistentHandler, '"%r%r'];
    end
    key  = [key, '[HKEY_CLASSES_ROOT\.', fileExt, ...
      '\Versions\MATLAB.', fileExt, '.' verStr, ']%r"FileVersionMS"=dword:', ...
      verHex, '%r"FileVersionLS"=dword:00000000%r%r'];
    
    % 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);

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

解决MATLAB不能设置为.m文件默认打开方式 的相关文章

  • 如何在 MATLAB 中可视化球体的交集?

    似乎这个问题在一些地方被问过 包括SO https stackoverflow com questions 35130336 draws the intersecting volume of two spheres in matlab 我最
  • 在 Excel 中打印 MATLAB 图窗并调整其大小

    我在 MATLAB 中有两个带有手柄的图形hFig1 and hFig2 我想将它们打印到 Excel 中的特定单元格 单元格 E3 和 I3 并将它们重新调整为 2 英寸 x 3 英寸 我尝试过使用 AddPictures对象处理程序和使
  • 在 MATLAB 中检索 spfun、cellfun、arrayfun 等中的元素索引

    有什么办法可以找回index调用函数的元素的cellfun arrayfun or spfun行为 即检索函数范围内元素的索引 为了简单起见 假设我有以下玩具示例 S spdiags 1 4 0 4 4 f spfun x 2 x S 它构
  • 计算数组中接下来的 n 个元素的乘积

    我想计算下一个的乘积n矩阵的相邻元素 号码n要相乘的元素数应在函数的输入中给出 例如 对于此输入 我应该从第一个开始计算每 3 个连续元素的乘积 p ind max product 1 2 2 1 3 1 3 这给出了 1 2 2 2 2
  • MATLAB 中时间戳过滤器的优化 - 处理非常大的数据集

    我正在 MATLAB 中编写一个程序 必须使用 MATLAB 并且不能真正使用 MEX 来过滤大量数据 我需要实现的过滤器之一要求我将时间戳向量与其他时间戳不会出现的已知 不良 时间列表进行比较 一个典型的时间戳向量有大约 2 000 00
  • Python 函数句柄 ala Matlab

    在 MATLAB 中可以创建function handles http www mathworks co uk help techdoc ref function handle html与类似的东西 myfun arglist body 这
  • MATLAB 中的多个捕获组

    我有一个包含数字或字母的字符串a 可能紧随其后的是r or l 在 MATLAB 中 以下正则表达式返回为 gt gt regexp 10r 0 9 a l r match ans 10r 我希望10 and r分开 因为我有两个捕获组 有
  • Matlab 字段名索引[重复]

    这个问题在这里已经有答案了 所以我有一个包含多个表的元胞数组 我试图访问表的第一个列名称 c table1 table2 table3 以下两行都给了我错误 fieldnames c 1 1 fieldnames c 1 1 Error i
  • MATLAB 教程中的 SIFT 实现

    我正在寻找 MATLAB 中的一些基本 SIFT 实现 我需要从第一原则来写它 另外 我正在寻找一些可以解释程序中发生的事情的内容 Vedali 的代码和 David Lowe 的代码超出了我的理解范围 如果您是 Matlab 用户 您一定
  • Matlab:如何更改矩阵的存储方式?从 1x1x3 到 1x3?

    我目前有 val 1 0 7216 val 2 0 7216 val 3 0 7216 但我想要 0 7216 0 716 0 721 我可以做什么样的操作来做到这一点 The reshape函数将在这里解决问题 Arrange the e
  • 使用符号求解器仅求解某些变量

    我正在尝试在 MATLAB 中求解包含 3 个变量和 5 个常量的方程组 是否可以使用solve求解三个变量 同时保持常量为符号而不用数值替换它们 当您使用SOLVE http www mathworks com access helpde
  • 如何在 MATLAB 中绘制 3D 曲面图?

    我有一个像这样的数据集 0 1 0 2 0 3 0 4 1 10 11 12 13 2 11 12 13 14 3 12 13 14 15 4 13 14 15 16 我想在 matlab 中绘制 3D 曲面图 使列标题位于 y 轴 行标题
  • 如何在文本集中创建所有字符组合?

    例如 我有这样的文本集 第 1 栏 a b 第 2 栏 l m n 第 3 栏 v w x y 我想将它们组合起来以获得如下输出 alv alw alx aly amv amw amx amy 这将输出 24 种文本组合 如果我只使用前两列
  • 了解 fminunc 参数和匿名函数、函数处理程序

    请多多包涵 问题在最后 我试图找出 fminunc 调用方式的差异 这个问题源于 Andrew Ng 在他的 Coursera 机器学习课程中的第 3 周材料 我正在回答这个问题 Matlab Andrew Ng 机器学习课程中 t cos
  • “Desort”向量(撤消排序)

    在Matlab中 sort返回排序后的向量和索引向量 显示哪个向量元素已移动到以下位置 v ix sort u Here v是一个包含所有元素的向量u 但已排序 ix是一个向量 显示每个元素的原始位置v in u 使用 Matlab 的语法
  • 图像处理 - 使用 opencv 进行服装分割

    我正在使用 opencv 进行服装特征识别 第一步 我需要通过从图像中移除脸部和手来分割 T 恤 任何建议表示赞赏 我建议采用以下方法 Use 阿德里安 罗斯布鲁克的用于检测皮肤的皮肤检测算法 谢谢罗莎 格隆奇以获得他的评论 在方差图上使用
  • 给定协方差矩阵,在Matlab中生成高斯随机变量

    Given a M x M期望的协方差 R 以及所需数量的样本向量 N计算一个N x M高斯随机向量 X在普通 MATLAB 中 即不能使用r mvnrnd MU SIGMA cases 不太确定如何解决这个问题 通常你需要一个协方差并且意
  • 如何将复杂的 csv 文件导入到 Matlab 中的数值向量

    我想知道我们应该如何读取由字符串 双精度数和字符等组成的复杂 csv 文件 例如 您能否提供一个可以在此 csv 文件中提取数值的成功命令 Click here http www ecb europa eu stats money yc d
  • Matlab Builder JA - 将 Matlab 编译成 Java jar - 免费版本?

    请记住 我对 Matlab 一无所知 Matlab Builder JA 允许开发人员构建 Matlab 应用程序并将其导出到 Java jar 中 太棒了 我只需要生成一个 jar 然后就可以从其他 java 代码中使用它 有谁知道单罐包
  • 如何在 MATLAB 中绘制纹理映射三角形?

    我有一个三角形 u v 图像中的坐标 我想在 3D 坐标处绘制这个三角形 X Y Z 与图像中的三角形进行纹理映射 Here u v X Y Z都是具有三个元素的向量 代表三角形的三个角 我有一个非常丑陋 缓慢且令人不满意的解决方案 其中我

随机推荐