MATLAB: An unhandled exception of type ‘System.Exception’ occurred in MWArray.dll

exception errornet assembly

Hello
I wrote a simple unction in Matlab 2017a and converted it to a .Net assembly.
This is the function:
function reshapeImg(imagePath)
image = imread(imagePath);
result = imcrop(image, [500, 1000, 1780, 1307]);
imwrite(result, imagePath);
end
When I try to run it inside VB.net code, I get this error with no additional information:
An unhandled exception of type 'System.Exception' occurred in MWArray.dll
I am suspicious about file operation commands because I have written another function which just reads files and handles some parameters as output, and I don't get this error.
Besides, I don't face any problem or error when I execute the function inside the Matlab itself.
How can I solve this?

Best Answer

I found the solution myself. A file can not be overwritten outside Matlab. it must be with a different name.