1. 前言

安装「Matlab R2017b」后,无法关联.m.fig文件,每次需要在MATLAB里边打开,而不能之间点击.m文件打开,十分麻烦。

2. 解决方案

1、首先,在Matlab R2017b中运行associateFiles.m文件,生成MatlabFileAssocFix.reg文件;

2、直接双击MatlabFileAssocFix.reg文件,如果有拦截,选择允许运行即可。

associateFiles.m文件源代码:

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')), 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);


未完 ......

点击访问原文(进入后根据右侧标签,快速定位到本文)

Matlab R2017b 关联 .m 和 .fig 文件的更多相关文章

  1. matlab直接运行fig文件时报错

    Matlab里面所的程序都是以.m的脚本文件形式保存的,所有运行的都是m文件.所以,对于guide生成的GUI程序,打开的方式有两种: 一:打开.m文件,点击m文件上的运行按钮,会自动弹出figure ...

  2. ubuntu16.04安装MATLAB R2017b步骤详解(附完整文件包)

    摘要:介绍在ubuntu16.04中从下载到安装成功的完整步骤.本文给出MATLAB R2017b(Linux系统)的完整安装包百度云盘下载地址,逐步介绍一种简单易行的安装方法,在桌面创建快捷方式,最 ...

  3. matlab从fig文件中提取数据

    如果你的fig文件中图像是由多条曲线绘制而成,比如说plot命令生成的,通过以下方式输出横坐标,纵坐标的取值 open('figname.fig'); lh = findall(gca, 'type' ...

  4. 获得MATLAB中FIG文件的矩阵

    在拓扑优化中,经常使用imagesc函数来显示最终的结果,往往会保存了Fig文件,却忘记保存mat文件. 根据已有的Fig文件,是可以找到其所显示矩阵.这个是我从fig数据结构中一层一层找到的,记录一 ...

  5. MATLAB R2017b安装及破解(安装详解)

    昨天知道有这个Matlab之后就开始想办法安装,今天为各位小伙伴解答昨天安装的过程,希望能够帮助到你们! 使用的镜像软件,我放在压缩包里面 如果你们感觉下载比较麻烦(可以直接发消息给我,我会发给你们的 ...

  6. ubuntu14.04安装MATLAB R2017b步骤详解

    转载:https://blog.csdn.net/qq_32892383/article/details/79670871 1. 前言最近由于项目原因,需要在ubuntu上安装MATLAB,在网上找了 ...

  7. Matlab R2017b 打开后一直显示“正在初始化”,导致无法运行命令

    1. 前言 Matlab R2017b打开后一直显示"正在初始化",导致无法运行命令. 2. 解决方案 1. 找到并记录授权文件license_standalone.lic的路径. ...

  8. 使用 matlab 产生GK101任意波数据文件的方法

    一.引言 MATLAB是由美国mathworks公司发布的主要面对科学计算.可视化以及交互式程序设计的高科技计算环境.它不但包含高效的数值计算.数据处理能力,而且简单易用,是工程师日常研发过程中不可缺 ...

  9. matlab的m程序转执行文件exe

    转换主要有两步: 第一步 设置编译器 在命令窗口输入 mbuild -setup 根据提示操作即可,.如下图我的设置 第二步 转换执行文件 命令行输入 mcc -m main   即可(输入 mcc ...

随机推荐

  1. avalon结合原生js tab切换

    <div class="fishqc-tap"> <div ms-class="[@codePic!=''&&@codeInfo!='' ...

  2. ICEM-模型导入失败的解决方法

    原视频下载地址:https://yunpan.cn/cxITx5uXY6dAp  访问密码 ade8

  3. Hutool工具类之HttpUtil使用Https

    关于Hutool工具类之HttpUtil如何使用可以参考官方文档Hutool之HttpUtil 其实使用Http和Https使用的方式是一样的. 建议大家可以看看HttpUtil的源码,感觉设计的挺不 ...

  4. PCA python 实现

    PCA 实现: 参考博客:https://blog.csdn.net/u013719780/article/details/78352262 from __future__ import print_ ...

  5. vue-使用keepAlive对上下拉刷新列表数据 和 滚动位置细节进行处理

    [前言] 使用vue处理项目中遇到列表页面时,有一些细节需要注意,这里总结一下,以便优化以后的代码.如下: 1. 使用mint-ui中的LoadMore组件上下拉刷新时,有时无法触发上拉加载更多的方法 ...

  6. 使用hwclock读取rtc中的时间时报错"hwclock: ioctl(RTC_RD_TIME) to /dev/rtc0 to read the time failed: No such device or address"如何处理?

    1. No such device or address 这一句表明当前的板子上没有这样的外设,检查设备树和硬件连接情况 2. 笔者是这样解决的 由于设备树中为rtc所指定的总线与硬件上的连接rtc的 ...

  7. SSIM (Structural SIMilarity) 结构相似性

    公式基于样本x和 y 之间的三个比较衡量:亮度 (luminance).对比度 (contrast) 和结构 (structure). 每次计算的时候都从图片上取一个 N*N的窗口,然后不断滑动窗口进 ...

  8. Linux_CentOS软件安装yum

    在 Linux 操作系统下,几乎所有的软件均通过 RPM 进行安装.卸载及管理等操作.RPM 的 全称为 Redhat Package Manager ,是由 Redhat 公司提出的,用于管理 Li ...

  9. Qt编写气体安全管理系统10-数据导出

    一.前言 数据导出一般指导出到excel表格,可能有部分用户还需要导出到pdf,因为pdf基本上不可编辑,防止用户重新编辑导出的数据,excel可能绝大部分用过电脑的人都知道,广为流行,主要就是微软的 ...

  10. Go并发编程实战 第2版 PDF (中文版带书签)

    Go并发编程实战 第2版 目录 第1章 初识Go语言 1 1.1 语言特性 1 1.2 安装和设置 2 1.3 工程结构 3 1.3.1 工作区 3 1.3.2 GOPATH 4 1.3.3 源码文件 ...