[英]MATLAB to C++: Coder: not consistent array dimension concatenation
改編此編碼器兼容解決方案中的代碼以讀取 csv 數據,我在 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)));
我的適應:
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);
.csv
以防萬一是 200x51 矩陣
在 matlab 中測試:按預期工作 - .csv
以 1:1 讀取,與csvread()
該錯誤在代碼生成期間彈出,據我所知,將sscanf
的結果寫入預分配的result
數組存在問題 - 但僅適用於 c 代碼。
附錄:只有整數值(1,1,1,...,0)
的行工作正常,實際浮點數(6.7308,38.7101,...,40.5999,0)
因上述錯誤而中斷。
remainder = fread(f, [1, Inf], '*char');
原來在這種情況下sizeA
參數不是可選的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.