📄 tffio.m
字号:
end
% check number of lines/columns to come
if (linec + rdimrows - 1) > linecount
error( ...
'BVQXtools:BadTTFCont', ...
'Too few lines to process ARRAY.' ...
);
end
% depending on datatype
switch (lower(rdata))
% built-in
case { ...
'char', 'int8', 'int16', 'int32', ...
'uint8', 'uint16', 'uint32', ...
'single', 'double', 'logical'}
% set function tokens
aconvopen = lower(rdata);
% specially deal with strings
case {'cstring', 'string'}
% get data to read
readdata = linecont(linec:linec + rdimrows - 1);
% create new array
rdcell = cell(rdim(1), rdimcols);
% treat correctly formatted lines differently
if ~isempty(rform) && ...
rform(1) ~= '%' && ...
rform(1) == rform(end)
% get formating encloser
nrf = rform(1);
% iterate over lines
for arc = 1:rdimrows
% match with \%([^\%]*)\%\s*
[sarra{1:3}] = regexp(readdata{arc}, ...
['\' nrf '([^\' nrf ']*)\' nrf '\s*']);
sarrt = sarra{3};
% no match found
if isempty(sarrt)
% fill cells
rdcell{arc, 1} = readdata{arc};
for acc = 2:rdimcols
rdcell{arc, acc} = '';
end
% matches found
else
% over cells
for acc = 1:rdimcols
% if match for cell
if length(sarrt) >= acc
% fill cee
rdcell{arc, acc} = ...
readdata{arc}( ...
sarrt{acc}(1, 1):sarrt{acc}(1, 2));
% no match for cell
else
% default
rdcell{arc, acc} = '';
end
end
end
end
% no correct delimiter
else
% set rdcell to data
rdcell = readdata;
end
% simply copy string lines (discard second dim)
try
eval(['tffcont.' rexpr '=rdcell; clear rdcell;']);
catch
error( ...
'BVQXtools:BadTFFCont', ...
'Could not copy string list into %s.', ...
rexpr ...
);
end
linec = linec + rdimrows;
rulec = rulec + 1;
continue;
% special
otherwise
aconvopen = ['array2' rdata];
end
% try conversion to double first
try
if rdimrows > 0
eval(['rarray(:, :) = [' ...
gluetostring(linecont(linec:linec + rdimrows - 1), ';') ...
'];']);
end
catch
error( ...
'BVQXtools:BadTFFCont', ...
'Cannot parse numeric ARRAY %s.', ...
rexpr ...
);
end
% try conversion now
if ~strcmp(aconvopen, 'double')
try
eval(['rarray=' aconvopen '(rarray);']);
catch
error( ...
'BVQXtools:ConversionFailed', ...
'Couldn''t convert ARRAY from double=>%s.', ...
aconvopen ...
);
end
end
% store array
try
eval(['tffcont.' rexpr '=rarray;']);
catch
error( ...
'BVQXtools:EvaluationError', ...
'Couldn''t store ARRAY into struct: ''%s''.', ...
rexpr ...
);
end
% increase linec
linec = linec + rdimrows;
% write access
else
% initialize array
larray = cell(rdimrows, 1);
carray = cell(1, rdimcols);
% get data to write
try
eval(['writedata=tffcont.' rexpr ';']);
catch
error( ...
'BVQXtools:EvaluationError', ...
'Data field evaluation error: ''%s''.', ...
rexpr ...
);
end
% check dim
if length(size(writedata)) ~= 2 || ...
any(size(writedata) ~= rdim)
error( ...
'BVQXtools:BadTFFSpec', ...
'ARRAY dimension mismatch.' ...
);
end
% paragraph array ?
if par
linecont{end+1} = '';
linecount = linecount + 1;
end
% depending on datatype
switch (lower(rdata))
% built-in
case { ...
'char', 'int8', 'int16', 'int32', ...
'uint8', 'uint16', 'uint32', ...
'single', 'double', 'logical'}
% set function tokens
aconvopen = lower(rdata);
% handle strings specifically
case {'cstring', 'string'}
% check array type
if ~iscell(writedata)
error( ...
'BVQXtools:BadTFFCont', ...
'String ARRAY must be of type cell.' ...
);
end
% just one columns
if rdimcols == 1 && ...
strcmp(rform, '%s')
% put strings into file
try
larray(1:end) = writedata;
catch
error( ...
'BVQXtools:AssignmentFailed', ...
'Could not copy string array from %s.', ...
rexpr ...
);
end
% more columns
else
% iterate over rows
numcols = size(writedata, 2);
for arc = 1:rdimrows
% format line
try
if numcols > 0
larray{arc} = deblank(sprintf([rform ' '], ...
writedata{arc, :}));
else
larray{arc} = '';
end
catch
error( ...
'BVQXtools:SprintfError', ...
'Invalid sprintf call: ''%s''->''%s''.', ...
['sprintf(''' rform ' '', ...)'], ...
lasterr ...
);
end
end
end
% put lines into file content
linecont(end + 1:end + rdimrows) = larray;
linecount = linecount + rdimrows;
rulec = rulec + 1;
continue;
% special
otherwise
aconvopen = [rdata '2array'];
end
% try conversion
if ~strcmpi(rdata, 'double')
try
eval(['writedata=double(' aconvopen '(writedata));']);
catch
error( ...
'BVQXtools:ConversionFailed', ...
'Couldn''t convert ARRAY from %s=>double.', ...
aconvopen ...
);
end
end
% combine data over rows
try
for rowc = 1:rdimrows
% build array elements
for colc = 1:rdimcols
carray{colc} = sprintf( ...
rform, writedata(rowc, colc));
end
% build entry with sprintf
larray{rowc} = deblank(sprintf([arf fds], carray{:}));
end
catch
error( ...
'BVQXtools:FormatError', ...
'Error formatting ARRAY Element: ''%s''.', ...
lasterr ...
);
end
% put lines into file content
linecont(end + 1:end + rdimrows) = larray;
linecount = linecount + rdimrows;
end
% begin of a loop
case {'bloop'}
% check loop var syntax (and extract dim, if needed)
[ldimmatcha{1:3}] = regexpi( ...
rexpr, '^([a-z][a-z_0-9]*)(\(\d+\))?');
ldimmatcht = ldimmatcha{3};
if isempty(ldimmatcht)
error( ...
'BVQXtools:BadBFFSpec', ...
'Invalid LOOP variable name given: ''%s''.', ...
rexpr ...
);
end
lvname = rexpr(ldimmatcht{1}(1, 1):ldimmatcht{1}(1, 2));
if size(ldimmatcht{1}, 1) > 1 && ...
ldimmatcht{1}(2, 2) > ldimmatcht{1}(2, 1)
try
eval(['ldim = [' rexpr(ldimmatcht{1}(2, 1):ldimmatcht{1}(2, 2)) '];']);
catch
ldim = 1;
end
else
ldim = 1;
end
% check whether appropriate entry in Loops.(...) exists
if ~isfield(loops, lvname) || ...
length(loops.(lvname)) < ldim
error( ...
'BVQXtools:BadBFFSpec', ...
'Invalid LOOP variable given (not found: ''%s''.', ...
rexpr ...
);
end
% get specific loop info
loopinfo = loops.(lvname)(ldim);
% check whether the loop should be entered at all
enterloop = true;
if ~isempty(rcond)
enterloop = false;
try
eval(['if ' rcond ',enterloop = true;end']);
catch
error( ...
'BVQXtools:BadExpression', ...
'Couldn''t evaluate COND expression: ''%s''.', ...
rcond ...
);
end
end
% if we're entering the loop
if enterloop
% try to resolve loop dim
try
loopinfo.dim = eval(tff_parsecode(loopinfo.dim));
catch
error( ...
'BVQXtools:BadExpression', ...
'Couldn''t evaluate LOOP.DIM expression: ''%s''.', ...
loopinfo.dim ...
);
end
% initialize loop counter
try
eval(['namevars.' rexpr '=1;']);
catch
error( ...
'BVQXtools:BadExpression', ...
'Error initializing LOOP counter ''%s'' to 1.', ...
rexpr ...
);
end
% only truly enter loop if dim is > 0
if loopinfo.dim > 0
% increase loop counter and keep track of loop variable name
loopc = loopc + 1;
loopi(loopc) = loopinfo;
loopx{loopc} = rexpr;
% otherwise
else
% skip loop
rulec = loopinfo.lastrule;
end
% we're not entering the loop
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -