解决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文件默认打开方式 的相关文章

  • 使用 python 在网络上部署 matlab 应用程序

    您好 我想使用 python 在网络上部署 matlab 应用程序 有没有办法做到这一点 我已按照数学工作网站上的文档将我的应用程序转换为 jar 文件 java 类 有人能指出我前进的正确方向吗 事实上 您的 Matlab 代码打包为 J
  • 在 C/C++ 中调用 MATLAB API

    我刚刚从某处听说 对于数值计算 MATLAB 确实提供了一些用户友好的 API 如果你在 C C 代码中调用这些 API 你可以显着加快计算速度 但我在MATLAB文档中没有找到这样的信息 例如http www mathworks com
  • 这是 `min` 和 `nanmin` 之间的区别; Matlab 中的“max”和“nanmax”?

    Matlab描述nanmin and nanmax像这样 NANMIN最小值 忽略NaNs NANMAX最大值 忽略NaNs 但实际上 min and max ignore NaNs too 那我应该使用哪个 根据我的测试 nanmin a
  • 如何从绘图处理程序中绘图?

    我有绘图的处理程序或图形的处理程序 例子 h plot 1 0 2 10 xx get h xx DisplayName Annotation 1x1 handle Color 0 0 1 LineStyle LineWidth 0 500
  • 同时重新排序和旋转图像的高效方法

    为了快速加载 jpeg 我为turbojpeg 实现了一个 mex wrapper 以有效地将 大 jpeg 读入 MATLAB 对于 4000x3000px 的图像 实际解码只需要大约 120 毫秒 而不是 5 毫秒 然而 像素顺序是 R
  • 在 Matlab 中显示有理数

    我有两个整数 m n 它们一起形成 m n 形式的有理数 现在我只想以这种理性的形式在 Matlab 中显示它们 我可以通过这样做来做到这一点 char sym m n 所以 如果 例如m 1 n 2 Matlab将显示1 2 然而 如果m
  • 频域和空间域的汉明滤波器

    我想通过在 MATLAB 中应用汉明滤波器来消除一维信号中的吉布斯伪影 我所拥有的是k1这是频域中的信号 我可以通过应用 DFT 来获取时域信号k1 s1 ifft ifftshift k1 该信号具有吉布斯伪影 现在 我想通过 A 乘以汉
  • 作为动画的八度情节点

    我有以下八度脚本 TOTAL POINTS 100 figure 1 for i 1 TOTAL POINTS randX rand 1 randY rand 1 scatter randX randY hold on endfor 当我运
  • 单元格的 Fieldnames 函数的等效项

    正如标题所说 只是想知道是否有一个函数可以用作字段名 http www mathworks co uk help matlab ref fieldnames html 但适用于单元格 所以如果我有类似的东西 a imread redsqua
  • 从 Java 运行 MATLAB 函数

    我在 MATLAB 中有一个 m 文件 我想从 Java 调用该文件 并以字符串或 Java 中的任何形式获取解决方案 这听起来很简单 但由于某种原因我无法让它发挥作用 我试过这个 matlab nosplash wait nodeskto
  • 如何获取MATLAB句柄对象的ID?

    当我尝试使用时出现问题MATLAB 句柄对象 http www mathworks com help techdoc ref handle html作为关键值MATLAB 容器 Map http www mathworks com help
  • 将数据提示堆栈放在轴标签顶部,并在轴位置发生更改后更新轴标签

    此问题仅适用于 unix matlab Windows 用户将无法重现该问题 我在尝试创建位于 y 轴标签顶部的数据提示时遇到问题 下图很能说明问题 正如您所看到的 在 ylabel 附近创建的数据提示将到达 ylabel 文本的底部 而期
  • Matlab颜色检测

    我试图一致地检测同一场景的图像之间的某种颜色 这个想法是根据颜色配置文件识别一组对象 因此 例如 如果给我一个带有绿色球的场景 并且我选择绿色作为我的调色板的一部分 我想要一个具有反映它检测到球的矩阵的函数 任何人都可以为这个项目推荐一些
  • 动态调整自定义刻度数

    Taking SO 的一个例子 https stackoverflow com a 7139485 97160 我想根据当前视图调整轴刻度 这是默认行为 除非设置自定义的刻度数 下图展示了由此产生的行为 左侧是默认行为 右侧是带有自定义刻度
  • 我如何编写一个名为 dedbi 的 MATLAB 函数,它将输入 xtx 作为字符串并返回另一个字符串 xtxx 作为输出。

    dedbi 反转单词 即 a 将被 z 替换 b 将被 y 替换 c 将被 x 替换 依此类推 dedbi 将对大写字母执行相同的操作 即将字符串 A 替换为 Z 将 B 替换为 Y 将 C 替换为 X 依此类推 如果我给函数这个字符串 a
  • 如何正确从表中删除 NaN 值

    在 Matlab 中阅读 Excel 电子表格后 不幸的是 我的结果表中包含了 NaN 例如这个 Excel 表格 将产生此表 其中出现额外的 NaN 列 我尝试使用以下代码片段删除 NaN measurementCells readtab
  • Matlab:条形图中缺少标签

    使用 Matlab 2012 和 2013 我发现设置XTickLabel on a bar图表最多只能使用 15 个柱 如果条形较多 则标签会丢失 如下所示 绘制 15 个条形图 N 15 x 1 N labels num2str x d
  • Matlab的导入函数的范围是什么?

    我正在尝试将一些用 Matlab 编写的代码转换为独立的 编译的 Matlab 应用程序 然而 在出现一些奇怪的错误之后 我意识到代码大量使用了从路径中添加和删除的操作 以避免多次使用多个具有相同名称 但结果 计算不同 的函数这一事实 环顾
  • matlab中更快的插值方法

    我正在使用 interp1 来插值一些数据 temp 4 30 4 rand 365 10 depth 1 10 dz 0 5 define new depth interval bthD min depth dz max depth ne
  • FMINCON 的替代方案

    除了 fmincon 之外还有其他更快 更高效的求解器吗 我正在使用 fmincon 来解决特定问题 但对于中等大小的向量变量来说 我的内存不足 我也没有任何超级计算机或云计算选项可供使用 我知道任何替代解决方案仍然会耗尽内存 但我只是想看

随机推荐