我正在使用GP'(遗传编程)过程生成表达式,我想测试后代候选者在生成后是否具有有效的Matlab语法。这意味着在无效的情况下,主进程不会受到干扰,从而导致一种具有子进程的解决方案。是否有直接的方法来执行此操作,而不是在其中编写带有eval(表达式)的m文件,从Matlab内部调用它,在变量中编写响应并使用isnumeric分析它(如果这样的管道甚至可以工作)。
答案 0 :(得分:0)
我有以下部分解决方案,但两个建议的系统字符串都有它们的错误:
% WRITE m-file WITH EXPRESSION AS STRING IN P_prim
P_prim_name = 'test_verification.m';
fileID = fopen(P_prim_name, 'wt');
formatSpec = '%s';
L1 = 'F = ';
fprintf(fileID,formatSpec,L1);
formatSpec = '''%s''';
fprintf(fileID,formatSpec,P_prim);
L2 = ';';
formatSpec = '%s\n';
fprintf(fileID,formatSpec,L2);
L3 = 'z = 2; Matlab_return = eval(F);';
fprintf(fileID,formatSpec,L3);
fclose(fileID);
% MAKE SYSTEM-STRING AND CALL
Matlab_systemstring = strcat('matlab -nodesktop -r "run(''', P_prim_name, '''); save(''test_verification_result'', ''Matlab_return''); exit"');
% halt before getting to exit if expression is invalid
Matlab_systemstring = strcat('matlab -nodesktop -r "try, run(''', P_prim_name, '''); save(''test_verification_result'', ''Matlab_return''); catch, exit, end, exit"');
% generates some numeric values even if expression is invalid
system(Matlab_systemstring);
% LOAD mat-file AND ANALYSE VARIABLE
clear Matlab_return
load('test_verification_result.mat'); disp(['Matlab_return = ', num2str(Matlab_return)]);
if isnumeric(Matlab_return) == 1;
disp('Matlab_return IS numeric => expression valid');
else;
disp('Matlab_return IS NOT numeric => expression invalid');
end