fread size mismatch in Code Generation
4 views (last 30 days)
Show older comments
Hello!
I am trying to build .mex files from my code but the MATLAB Coder App fails at this line and other similar ones:
fixed_size_matrix(ii,jj) = fread(fileID, 1, '*uint8');
The error is:
Size mismatch ([1 x 1] ~= [:? x 1])
I guess this is because fread may return [] if the file end is reached. I tried to do a wrapper function which tests for an empty return value and then generates a zero vector of appropriate size and type. But of course code generation does not support dynamic typing.
What would be the appropriate solution here?
0 Comments
Answers (2)
Nagini Venkata Krishna Kumari Palem
on 31 Mar 2017
From an initial review of your code, I understand that you are trying to store extracted data into a fixed size matrix.
Here, in 'fread' function you are extracting 1-byte of data (1x1 size), and trying to store it in a fixed size matrix (more than 1x1 size), which is logically incorrect. Instead, you can store it as follows,
fixed_size_matrix(:,jj) = fread(fileID, 1, '*uint8');
or
fixed_size_matrix(ii,:) = fread(fileID, 1, '*uint8');
1 Comment
Walter Roberson
on 29 Dec 2017
I disagree with the above. The user is trying to read exactly one byte and store it into exactly one location. However, it is true that fread() in general might return smaller than the requested size so Yes theoretically the original code was incorrect.
Isaac
on 29 Dec 2017
I've ran into this too (in Matlab 2016b) and I got around it by:
tempNum = fread(fileID, 1, '*uint8');
fixed_size_matrix(ii,jj) = tempNum(1,1);
The way you wrote the code should work as is instead of needing this intermediate variable, but this works as a temporary solution.
1 Comment
Walter Roberson
on 29 Dec 2017
You could also then use
if isempty(tempNum)
to check for EOF. Checking EOF is in general a good idea: it is better not to assume that your file cannot have been corrupted somehow (including by running out of disk space for example.)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!