How to deal with corrupted matlabprefs?

20 views (last 30 days)
I run a matlab-script that use batch processing to collect sample from different sensors at same time.
Some sensors are connect using UDP protocol, modbus, and serial port.
As a rule, they work fine. However, sometimes I have an error related to matlabprefs.mat.
As a consequence, one of the sensors can stop to collect data, i.e., I can not read correctly form a specific serial port, and in the next time I run the same matlab script, it will fail producing some complaining about "matlabprefs.mat" (see example below).
I have this problem frequently.
I am affraid becasue If I can not deal properlly with this problem,I can not trust on matlab to control my data sample system.
Please, could you help me to figure out a deffinitive solution for this issue?
Warning: Initializing MATLAB Graphics failed.
This indicates a potentially serious problem in your MATLAB setup, which should be resolved as soon as possible. Error detected was:
MATLAB:load:unableToReadMatFile
Unable to read MAT-file C:\Users\user\AppData\Roaming\MathWorks\MATLAB\R2023a\matlabprefs.mat. File might be corrupt.
> In hgrc (line 151)
In matlab.graphics.internal.initialize (line 15)
  9 Comments
Walter Roberson
Walter Roberson on 23 Aug 2024
What you are asking about happens to be beyond the experience of @Steven Lord . Steven Lord happens to work for Mathworks, but when he posts here, he posts as a volunteer. He is recommending that you contact Mathworks Technical Support.
LucasBar
LucasBar on 23 Aug 2024
Ok, I just have to say thanks to @Steven Lord.
I already posted the issue on Mathworks Techinical Support.
My best regards

Sign in to comment.

Accepted Answer

LucasBar
LucasBar on 10 Sep 2024
I had a feedback from matlab support team.
see below the solution:
"Hello Lucas,
I am Srivatsav and am writing in reference to your Technical Support Case #07229140 regarding 'How to deal with corrupted matlabprefs?'. I understand that you are facing a “MATLAB:load:unableToReadMatfile” error while trying to execute your scripts in a parallel setup. I apologize for any inconvenience this might have caused.
Thank you for proactively sharing all the required files and information. I have been through your source files, and they were super helpful in narrowing down the issue. In your scripts, you are creating a “serialport” object which is later cleared at the very end of each script. The delete operation on the “serialport” object currently updates the preferences file (‘matlabprefs.mat’) unfortunately using APIs that do not currently support parallel execution. This is the root cause of the issue, and our development team is aware of this issue now and will consider it in a future release.
The possible workaround to avoid this issue is to create the ‘serialport’ objects once outside the batch execution and pass them as arguments to the ‘batch’ function to run the script. This will avoid calling delete operation on the ‘serialport’ object in parallel. You are already passing some arguments to the batch function, so you are aware of this functionality, but in case you would like a reminder, here is the documentation page discussing this:
I understand that this requires some refactoring and may not be the solution you were expecting, and I apologize for that.
I will go ahead and mark this case as closed. However, please do not hesitate to reach out again if you have any follow-up questions or comments regarding this issue and I will re-open the case to assist you further. Thank you for your patience.
Sincerely,
Srivatsav Gunisetty
MathWorks Technical Support"

More Answers (1)

Animesh
Animesh on 19 Aug 2024
  2 Comments
LucasBar
LucasBar on 19 Aug 2024
"removing the file helps for some time"
I know this might be a shortcut, but is not a solution. Once the error occurs, it will end the data processing and will not allow matlab starts correctly again. It is not desired that the program starts again, because there is a production chain linked to the script. Imagine this script running an airplane. Matlab should be able to fix this error, without having to restart matlab.
LucasBar
LucasBar on 19 Aug 2024
We can not just remove the result of an error without solve the error because this error will came back.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!