various size mismatch errors from fread when using Matlab coder
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello
I am using R2018a with the Matlab coder.
I have a working set of Matlab routines that I am trying to convert to C code as a standalone executable.
When I run the code generator, I sometimes get size mismatch errors on fread function calls.
function [dateStr, version, PW, loopback, encType, masterLogCfgTbl, dataTables, msgstruct, p] = readMsgFile(fname, p)
% define sizes of structures
dataTables.MPT_FMT_TBL.mptData = defineConfigTblsForMatlab();
msgstruct = createMsgstruct();
:
%% read sensor data (part of the mptdata structure)
numData = fread(mfid, 1, '*uint32');
for n = 1 : numData(1)
dataTables.MPT_FMT_TBL.mptData(n).mnemonic = fread(mfid, 12, '*uint8')'; % Note that there are no errors on any
dataTables.MPT_FMT_TBL.mptData(n).length = fread(mfid, 1, '*uint32'); % of these lines.
dataTables.MPT_FMT_TBL.mptData(n).scale_factor = fread(mfid, 1, '*single');
dataTables.MPT_FMT_TBL.mptData(n).low_limit = fread(mfid, 1, '*single');
dataTables.MPT_FMT_TBL.mptData(n).high_limit = fread(mfid, 1, '*single');
dataTables.MPT_FMT_TBL.mptData(n).data_index = fread(mfid, 1, '*uint32');
dataTables.MPT_FMT_TBL.mptData(n).units = fread(mfid, 8, '*uint8');
dataTables.MPT_FMT_TBL.mptData(n).disp_length = fread(mfid, 1, '*uint32');
dataTables.MPT_FMT_TBL.mptData(n).disp_places = fread(mfid, 1, '*uint32');
dataTables.MPT_FMT_TBL.mptData(n).print_ctrl = fread(mfid, 1, '*uint32');
dataTables.MPT_FMT_TBL.mptData(n).scale_type = fread(mfid, 1, '*uint32');
dataTables.MPT_FMT_TBL.mptData(n).data = fread(mfid, 1, '*single');
end
%% read message arrays
% Note: the freads followed by reshapes are due to the auto coder not supporting reading into a 2D indexed variable
% low inc non rotating table
p.message.nrot.low.nFID = fread(mfid, 1, '*uint32'); % numFIDs used in table This is line 67
s = fread(mfid, 2, '*uint32')'; % read size of table
p.message.nrot.low.msg = fread(mfid, s(1) * s(2), '*uint32'); % just lo FID msgs
p.message.nrot.low.msg = reshape(p.message.nrot.low.msg, s(1), s(2)); % This is line 70
msgstruct.nrot.loFID = p.message.nrot.low.msg; % just lo FID msgs
msgstruct.nrot.loFIDfmt = fread(mfid, s(1) * (s(2)+1), '*int16'); % just lo FID formats (formats have one extra row value)
msgstruct.nrot.loFIDfmt = reshape(msgstruct.nrot.loFIDfmt, s(1), s(2)+1);
% high inc non rotating table
tu32 = fread(mfid, 1, '*uint32'); % numFIDs used in table This is line 77
p.message.nrot.high.nFID = tu32(1,1); % numFIDs used in table
s = fread(mfid, 2, '*uint32'); % read size of table
p.message.nrot.high.msg = fread(mfid, s(1) * s(2), '*uint32'); % just hi FID msgs This is line 80
p.message.nrot.high.msg = reshape(p.message.nrot.high.msg, s(1), s(2));
msgstruct.nrot.hiFID = p.message.nrot.high.msg; % just hi FID msgs
msgstruct.nrot.hiFIDfmt = fread(mfid, s(1) * (s(2)+1), '*int16'); % just hi FID formats (formats have one extra row value)
msgstruct.nrot.hiFIDfmt = reshape(msgstruct.nrot.hiFIDfmt, s(1), s(2)+1);
% These functions are in separate files
function p = createToolOnlyPstruct()
p = struct;
p.sig = struct;
p.sig.PW = 0;
p.sig.delay = 0;
p.intfc = struct;
p.intfc.fs = 0;
p.message = struct;
p.message.sync1 = zeros(1, 38, 'logical');
p.message.sync2 = zeros(1, 26, 'logical');
p.message.nSync1 = int32(38);
p.message.nSync2 = int32(26);
p.message.nrot = struct;
p.message.nrot.low = struct;
p.message.nrot.low.nFID = uint32(0);
p.message.nrot.low.msg = zeros(5, 10, 'uint32');
p.message.nrot.high = struct;
p.message.nrot.high.nFID = uint32(0);
p.message.nrot.high.msg = zeros(5, 10, 'uint32');
p.message.rot = struct;
p.message.rot.nFID = uint32(0);
p.message.rot.msg = zeros(5, 10, 'uint32');
p.message.alert = struct;
p.message.alert.nFID = uint32(0);
p.message.alert.msg = zeros(1, 10, 'uint32');
p.message.nMsg = zeros(1, 5, 'uint32');
p.message.nFID = 0;
p.message.loopback = uint16(0);
p.message.FIDnums = zeros(1, 5, 'uint32');
p.message.iSync = zeros(1, 5, 'uint32');
p.message.rSync = zeros(1, 5, 'uint32');
p.message.rpt = zeros(1, 5, 'uint32');
function msgstruct = createMsgstruct()
msgstruct = struct;
msgstruct.nrot = struct;
msgstruct.nrot.loFID = zeros(5, 10, 'uint32');
msgstruct.nrot.hiFID = zeros(5, 10, 'uint32');
msgstruct.nrot.loFIDfmt = zeros(5, 11, 'int16');
msgstruct.nrot.hiFIDfmt = zeros(5, 11, 'int16');
msgstruct.rot = struct;
msgstruct.rot.FID = zeros(5, 10, 'uint32');
msgstruct.rot.FIDfmt = zeros(5, 11, 'int16');
msgstruct.alert = struct;
msgstruct.alert.FID = zeros(1, 10, 'uint32');
msgstruct.alert.FIDfmt = zeros(1, 11, 'int16');
function mptData = defineConfigTblsForMatlab()
SIUDATANUM = 56;
% define file structures
mptData.mnemonic = zeros(1, 12, 'uint8'); % Data word mnemonic - NOTE: well model has 12 char fixed size, MFT has 8?
mptData.length = zeros(1, 1, 'uint32'); % Data length in bits (up to 32)
mptData.scale_factor = zeros(1, 1, 'single'); % Scale factor
mptData.low_limit = zeros(1, 1, 'single'); % Scaling low limit
mptData.high_limit = zeros(1, 1, 'single'); % Scaling high limit
mptData.data_index = zeros(1, 1, 'uint32'); % Location of data word in the MPT data array
mptData.units = zeros(1, 8, 'uint8'); % Units label
mptData.disp_length = zeros(1, 1, 'uint32'); % Define total length of number in display/printout
mptData.disp_places = zeros(1, 1, 'uint32'); % Define number of decimal places in display/printout
mptData.print_ctrl = zeros(1, 1, 'uint32'); % 0 = unrestricted printout, 1 = restricted printout
mptData.scale_type = zeros(1, 1, 'uint32'); % 0 = no scaling, 1 = exclusive scaling, 2 = inclusive scaling
mptData = repmat(mptData, 1, SIUDATANUM); % create array of structures with SIUDATANUM entries
When the coder runs, I get the error
??? Size mismatch (size [5 x 10] ~= size [:? x 1]).
The size to the left is the size of the left-hand side of the assignment.
Error in ==> readMsgFile Line: 80 Column: 2
1. Notice that at lines 67 down to line 77, similar code has no errors. (Line numbers are in comment field).
2. At line 77 I had to use the answer given in "fread size mismatch in Code Gen" to work, but it is not needed and works correctly on line 67.
3. At line 69, the coder has no error, but at line 80 it does, even though the sizes of the data are the same.
4. At the top of the listing in the for loop, there are no errors although the freads are for both single and vector values of different size types.
4. The lines with calls to the reshape function were put in to correct other coder errors about size errors.
I don't understand the (seemingly) inconsistent behaviour of the coder.
I also don't understand why there is any question about the size of the fread output. For example, on line 77 fread is reading 1 value of uint32. If I read it directly into the structure, I get the same error as we see on line 80. I have to use the fix mentioned above (item 2) to get it to work.
I have tried declaring a temporary variable of zeros of size (5, 10) and making it the output of the fread at line 80. Same error.
I tried putting in fixed numbers for the varable sizes. Same error. (Wouldn't the coder flag the earlier lines with varable read counts as errors?)
Thank you for any help.
0 comentarios
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!