script . , script, , (, for
end
, ...
). , , , , .
:
function execute_script(fname)
fid = fopen(fname,'rt');
n = 0;
while ~feof(fid)
cmd = fgetl(fid);
n = n+1;
if ~isempty(cmd)
try
evalin('caller',cmd);
catch exception
disp(['Error occurred executing line number ',num2str(n),': ',exception.message]);
end
end
end
, : , evalin
. . .
, testscript.m
:
A = 1;
B = 2+C; % This line needs a variable not defined in the script!
D = 5;
MATLAB:
>> execute_script('testscript.m')
Error occurred executing line number 2: Undefined function or variable 'C'.
>> whos
Name Size Bytes Class Attributes
A 1x1 8 double
D 1x1 8 double
A
D
. C
:
>> C=0;
>> execute_script('testscript.m')
>> whos
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 8 double
C 1x1 8 double
D 1x1 8 double
C
script B
.