用途:把已移除(调用会报错)或不推荐(仍可用但已有更好 API)的 MATLAB 函数与反模式,替换为现代等价写法。该技能是“解决方”,check_matlab_code(MATLAB 的 Code Analyzer)是“检测方”。
触发时机:check_matlab_code 报告生命周期诊断,如“X is not recommended”(info)、“X will be removed in a future release”(warning)、“X has been removed”(error,当前即报错);代码使用了 Code Analyzer 未标记的不推荐模式(containers.Map、bsxfun、str2num、eval、clear all);用户要求现代化、迁移或更新旧 MATLAB 代码;使用下方速查表中的函数。移除类报错应优先处理。
主要迁移:csvread/dlmread/csvwrite/dlmwrite → readmatrix/writematrix(R2019a);xlsread/xlswrite → readtable/readmatrix/writetable/writematrix;datenum/datestr/now → datetime(R2022b 起不推荐);eval/evalc/evalin → 动态字段名、函数句柄/feval、元胞数组等结构化替代;str2num → str2num(text, Evaluation="restricted");uicontrol → uibutton/uidropdown 等;guide → appdesigner;strmatch → startsWith/matches;clear all → clearvars。datestr 的现代替代是 string(dt)/char(dt) 或设置 Format,注意格式符不同(datetime 中 MM=月、mm=分)。
现代模式:优先 readtable/groupsummary 的表格工作流、字符串数组、arguments 块。新代码从一开始就用现代写法。
用法约定:先运行 check_matlab_code,再扫描源码中分析器可能漏报的模式;containers.Map 不被标记,其属性默认值只会有句柄共享警告,应视为迁移触发点;subplot 未废弃,tiledlayout/nexttile 只是新代码的偏好替代,不要要求替换正常代码。迁移后应测试新旧行为是否等价。旧 .m 文件常为 Windows-1252 编码,编辑后需确认非 ASCII 字符未被改写。分领域细节见 references 下对应参考文件。
不适用:不做广泛代码质量审查、运行调试(移除函数报错除外)、性能优化(交给 matlab-optimize-performance)、参数验证编写与迁移(交给 matlab-validate-function-arguments)。