Matlab - 在每一行之后执行操作

Yuv*_*ler 0 matlab

是否有一种优雅的方式告诉matlab在执行某个脚本中的每一行后执行预定义的操作?优雅我的意思是在每一行之后都没有调用动作,而是在脚本开头给出一个简单的命令.

示例:操作 - > disp('Performing Action');

脚本:

a = 1;
b = 2;
c = 3;
Run Code Online (Sandbox Code Playgroud)

因此,期望的结果是在每次分配(a,b和c)之后,将执行disp()命令.

Lui*_*ndo 5

您可以自动创建一个修改过的文件,其中包含每行末尾所需的操作:

action = 'disp(''Performing Action'');'; %// action to be added at the end of each line
file = 'script.m';                       %// original script
file_out = 'script_out.m';               %// modified script with action added 
x = importdata(file);                    %// cell array of strings. Each line of the
                                         %// original file is a string
x = strcat(x, {' ; '}, action);          %// add action at the end of each string,
                                         %// preceded with `;` in case the original line
                                         %// didn't include that
fid = fopen(file_out, 'w');
fprintf(fid, '%s\n', x{:});              %// write output file
fclose(fid);
Run Code Online (Sandbox Code Playgroud)

  • @Kostas同意:_non-elegant_根据OP的定义.但是_automatic_ :-) (3认同)