adapting the code from this coder-compatible solution to read csv data i ran into the following issue during the runtime issue check of Matlab Coder:
Error using cat>>check_non_axis_size (line 283)
Dimensions of arrays being concatenated are not consistent.
Error in cat>>cat_impl (line 102)
check_non_axis_size(isempty, i, sizes{i}, varargin{:});
Error in cat (line 22)
result = cat_impl(@always_isempty_matrix, axis, varargin{:});
Error in readCsv (line 28)
coder.ceval('sscanf', [token, NULL], ['%lf', NULL], coder.wref(result(k)));
my adaptation:
function result = readCsv(filepath, rows, columns)
NULL = char(0);
fid = fopen(filepath, 'r');
% read entire file into char array
remainder = fread(fid, '*char');
% preallocation for speedup
result = coder.nullcopy(zeros(columns,rows));
k = 1;
while ~isempty(remainder)
% comma, newline
delimiters = [',', char(10)];
% strtok ignores leading delimiter,
% returns chars upto, but not including,
% the next delimiter
[token,remainder] = strtok(remainder, delimiters);
% string to double conversion
% no need to worry about return type / order
% since we only look at one token at a time
if coder.target('MATLAB')
result(k) = sscanf(token, '%f');
else
coder.ceval('sscanf', [token, NULL], ['%lf', NULL], coder.wref(result(k)));
end
k = k + 1;
end
% workaround for filling column-major but breaks on single-line csv
result = reshape(result,rows, [])';
disp(k)
fclose(fid);
the .csv
in case is a 200x51 matrix
testing in matlab: works as expected - the .csv
is read 1:1 as with csvread()
the error pops up during code generation, and as far as I understand, an issue with writing the result of sscanf
into the preallocated result
array - but only for the c code.
Addendum: a line with only integer values (1,1,1,...,0)
works fine, a line with actual floats (6.7308,38.7101,...,40.5999,0)
breaks with the aforementioned error.
remainder = fread(f, [1, Inf], '*char');
原来在这种情况下sizeA
参数不是可选的
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.