< p >
Dev-iL 使用java等提供了一个好的答案...... 在这里,我提供另一种方法,它不使用java或编辑器,而是在请求时读取文件并评估语句。
为了使其工作,它需要先保存文件(这不是交互式运行单元/代码块的先决条件,我很感激)。
无论如何,我认为这是一个“奇怪”的问题,并且想尝试回答。
这是我的脚本(cellScript.m),其中包含代码块(请注意,我已经在“%%”后面给每个块命名):
disp ( 'running cell A' );
disp ( 'finished cell A' );
disp ( 'running cell B' );
disp ( 'finished cell B' );
disp ( 'running cell C' );
for ii=1:100
aa(ii) = randi(1);
end
disp ( 'past 1st coment' );
disp ( 'past indented comment' );
disp ( 'past block comment' );
disp ( 'past multiple lines of comments' );
disp ( 'finished cell C' );
disp ( 'running cell D' );
disp ( 'finished cell D' );
disp ( 'running cell E' );
disp ( 'finished cell E' );
我创建了一个类来完成所需的工作(我将其称为cellRunner.m)。
classdef cellRunner < handle
properties ( SetAccess = private )
fileName
fileInfo
cellInfo
cellNames = {};
end
methods
function obj = cellRunner ( file ) % constructor
if nargin == 0
obj.fileName = 'cellScript.m';
else
obj.fileName = file;
end
obj.parseFile();
end
function obj = parseFile ( obj )
if ~isempty ( obj.fileInfo )
if isequal ( obj.fileInfo, dir ( obj.fileName ) )
return
end
end
obj.fileInfo = dir ( obj.fileName );
fid = fopen ( obj.fileName );
if fid ~= -1
index = 0;
inCell = false;
lines = cell(0);
while ( true )
line = fgetl ( fid );
if line == -1; break; end
sLine = strtrim ( line );
if length ( sLine ) > 2 && strcmp ( sLine(1:2), '%%' )
if index > 0
obj.cellInfo{index} = lines;
end
index = index + 1;
obj.cellNames{index} = strtrim ( sLine(3:end) );
lines = cell(0);
inCell = true;
elseif inCell
lines{end+1} = line;
end
end
if index > 0
obj.cellInfo{index} = lines;
end
fclose ( fid );
else
error ( 'cellRunner:fileError', 'unable to read file' );
end
end
function obj = runCell ( obj, arg )
obj.parseFile();
if ischar ( arg )
index = strcmp ( arg, obj.cellNames );
if ~any ( index )
error ( 'cellRunner:notFound', '%s not found', arg );
end
else
index = arg;
if index < 1 || index > length ( obj.cellInfo )
error ( 'cellRunner:notFound', 'Index %d not found', arg );
end
end
commands = obj.cellInfo{index}{1};
inBlock = false;
for ii=2:length(obj.cellInfo{index})
nextLine = strtrim ( obj.cellInfo{index}{ii} );
if inBlock
if length ( nextLine ) == 2 && strcmp ( nextLine, '%}' );
inBlock = false;
end
continue
end
if length ( nextLine ) == 2 && strcmp ( nextLine, '%{' );
inBlock = true;
continue
end
if length ( nextLine ) >= 1 && strcmp ( nextLine(1), '%' )
continue;
end
commands = sprintf ( '%s;%s', commands, obj.cellInfo{index}{ii} );
end
evalin('base',commands);
end
end
end
代码随后按以下方式使用:
obj.cellRunner();
obj.runCell ( 'cellC' );
obj.runCell ( 3 );
注意:为使此功能生效,文件必须被保存。
示例运行:
whos
obj = cellRunner ( 'cellScript.m' );
obj.runCell ( 'cellC' );
running cell C
past 1st coment
past indented comment
past block comment
past multiple lines of comments
finished cell C
whos
Name Size Bytes Class Attributes
aa 1x100 800 double
ans 1x1 112 cellRunner
ii 1x1 8 double
obj 1x1 112 cellRunner
注1 - 为什么要使用handle类?我从handle类继承,因为我只想要已读取的文件数据的一个副本 - 参见此问题中答案1,了解何时使用值/句柄类的优秀概述。
%%
后面放置的文本将在编辑器中以粗体突出显示),然后我混合在控制台中输入/尝试的内容和在编辑器中执行完整单元格的内容。如果我得到一系列满意的命令,我就从命令历史记录中拖动它到编辑器中,创建一个新单元格,给它一个好名字,这样我以后可以轻松找到它,然后继续我的下一段代码。 - Hoki