Clear Filters
Clear Filters

imwrite is corrupting a mex file when I run it

4 views (last 30 days)
Hi everyone,
I'm using Matlab 2023a to convert proprietary .nd2 files into 3D tiffs. I'm saving these tiffs by saving single 2D planes at a time and appending new layers as I load them, all by using imwrite. My program is rather long, so I'll outline the part of the pipeline before the chunk of code responsible for my error:
Generate FilePointer, ImagePointer, and ImageReadOut objects to access the .nd2 file. I use the function ND2Open to do so. It should be noted that I don't close these pointers until I'm done and worry this might be part of the reason I'm crashing.
I then select data of interest from my .nd2, then generate 2D matrices and begin saving them. The following is the code I use to save my tiffs:
currSaveDir = fullfile('My Directory','my_file_name.tif');
for indVec = 1:10
baseMat = rand(50,50); % In reality, I have code generating a 2D matrix from my original .nd2 at this point
if indVec == 1
imwrite(baseMat, currSaveDir);
else
imwrite(baseMat, currSaveDir, 'WriteMode', 'append')
end
end
After running this successfully for several data sets, my MATLAB crashed. Now I'm getting the following error:
Invalid MEX-file 'C:\Program Files\MATLAB\R2023a\toolbox\matlab\imagesci\private\wtifc.mexw64': The specified module could not be found.
Error in writetif (line 110)
wtifc(data, map, filename, writemode, colorspace, required_tags);
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in Copy_of_nd2Sideview (line 157)
imwrite(baseMat(:,:,currChan), currSaveDir);
I should note that this error persists whenever I try to save any .tif using imwrite. There IS a file called wtifc.mexw64 in the specified directory, but it's compiled so I have no idea what it's supposed to look like. I also had this issue in MATLAB 2022a
Does anybody understand what's going on? Is there an easy way to repair this file? A clean install of MATLAB did the trick before, but I'd like to know why I'm corrupting my MATLAB to begin with!
Thanks!
EDIT: As mentioned in a comment below, I'm accessing files located on an external hard drive connected via USB 3. This hard drive occasionally disconnects. My error could somehow be related to a disconnection event like this. It's still not clear why the above error would be the one that pops up, nor why the error persists when saving any tif using imwrite (regardless of origin or destination).

Answers (2)

Sachin Lodhi
Sachin Lodhi on 18 Sep 2023
Hello Remi,
It seems that you are experiencing an issue with saving large “tiff” files in MATLAB. After conducting some research, I came across a similar question in the MATLAB Answers community. According to the information provided, the “imwrite” function in MATLAB has a default limit of processing input data up to 2 GB. If your input data exceeds this limit, you will need to modify the value of the “RowsPerStrip” parameter.
The “RowsPerStrip” parameter determines the number of rows included in each strip and should be set as a scalar value, which is a multiple of “8”. By explicitly setting this parameter to a smaller value, you should be able to handle larger tiff files without encountering errors.
I recommend referring to the following links for more detailed information:
I hope this information helps you in resolving the error.
  2 Comments
DGM
DGM on 18 Sep 2023
Edited: DGM on 18 Sep 2023
This doesn't seem to be related to the question. OP is having a problem with a specific precompiled builtin file that's corrupt. OP ran some code that crashed MATLAB, now wtifc.mex64 is corrupt or unreadable. Nobody mentioned file size or anything, and at this point, file format is irrelevant if the necessary support functions are corrupt.
The question is
  1. how was this even possible? (no clue)
  2. how to fix it (probably reinstall or use backups)
  3. how to prevent it (see #1)
See also:
Remi Boros
Remi Boros on 18 Sep 2023
Hello all,
Thanks for taking the time to respond! As DGM suggests, the error persisted regardless of tif size. I was also saving/appending 2D tifs rather than 3D tifs with sizes no more than 1MB.
I attempted a clean install and the issue persisted for a few days. The problem appeared to go away when I made a matrix of random elements and saved it as a tif BEFORE running my code. The "fix" was probably serendipitous, but here's what I wrote in case it works for someone else.
temp = rand(50,50);
imwrite(temp,'test.tif');
For some reason, running this before running anything else/adding other folders to path fixed my mex file/worked.
I left out an important detail that may be responsible for my crash: I am accessing data stored on a hard drive in an external dock connected to my computer via USB 3.0. This dock periodically loses connection with my computer if I'm not actively accessing files on the host hard drive. It could be that the drive disconnects while running my code. I don't know if this could lead to the problem I describe, but I suspect that this is important.
Thanks again!

Sign in to comment.


Image Analyst
Image Analyst on 19 Sep 2023
I believe imwrite, when saving tiff files expects them to be uint8 in the range of 0-255 or uint16 in the range 0-65535. You're trying to pass it floating point numbers in the range 0-1. This should work
temp = uint8(randi(255, 50, 50)); % A uint8 variable.
imwrite(temp, 'test.tif');
But of course if it's in the middle of saving and the connection to your destination drive is lost, that would be a problem as well.
  2 Comments
DGM
DGM on 19 Sep 2023
Edited: DGM on 19 Sep 2023
Imwrite will automatically fix a unit-scale float image when writing to image types which don't support float (i.e. all of them). So OP's use of rand() should be fine.
temp = rand(50,50); % unit-scale float
imwrite(temp, 'test.png'); % no need to convert
recovered = imread('test.png');
isequal(im2uint8(temp),recovered) % it's identical
ans = logical
1
imwrite(temp, 'test.tif'); % no need to convert
recovered = imread('test.tif');
isequal(im2uint8(temp),recovered) % it's identical
ans = logical
1
That doesn't explain why wtifc.mex64 had become corrupt or inaccessible. AFAIK, those files should be write-protected, and if it's a pathing or library issue, I don't know why that would get changed either, even if some part of the script execution were interrupted. Of course, all that means is that I don't know. .
Remi Boros
Remi Boros on 19 Sep 2023
Thanks again for the responses, all. Even if we didn't concretely resolve the issue/figure out why this error occurs, it was helpful to see everybody's input!

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!