How to deal with corrupted matlabprefs?

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

Are multiple files (running in parallel, using Parallel Computing Toolbox or some other mechanism) trying to modify the preferences or the settings simultaneously? If so we're going to need to see those files and know more about how you run them in parallel to be able to suggest how to avoid the file contention. If you don't feel comfortable posting that code on MATLAB Answers you could contact Technical Support directly using this link and ask them to help you determine what's causing the contention and how to avoid it.
You also said "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," after an error. What is the full and exact text of that error message (all the text displayed in red in the Command Window)? If there are any warning messages (displayed in orange) please show those as well.
Nothing of what you've showed jumps out at me as obviously something that could corrupt the preferences, but there is a lot of information that you haven't shown us. Given how much back-and-forth debugging I think this is likely to require you should probably contact Technical Support and work with them to determine the cause of the corruption. Send them a link to this MATLAB Answers thread as well as the contents of the MissMie_sensor_selector and sample_continuous_MissMie functions (plus whatever they'd need to run those two functions.)
LucasBar
LucasBar on 22 Aug 2024
Edited: LucasBar on 22 Aug 2024
Dear Steven Lord, I figure out what is causing this error: In case, some problem occurs in a serial port in a script running in batch(), like sensor restart, or batch script trying to read a not closed serial port. This error related to a COM serial port will corrupt the matlabprefs.mat file. The question is why amd how fix it without reboot matlab or remove files.
Okay, now that you know what happened to corrupt the preferences, please work with Technical Support so they can help you determine how to avoid or work around the problem (and can report it to the development staff to be fixed.)
Why does matlab corrupt this file?
I don't know. It's a bug somewhere (in the parallel code, in the serial port code, in the preference handling code, or perhaps somewhere else.) Without reproduction steps it's going to be difficult or impossible to isolate it and offer a workaround and/or a solution. That's why I recommend contacting Technical Support.
It does not solve the Matlab problem. It can affect many other users, that can migrate to python to avoid it. Now, we are doing it. We are migrating our system to python until this issue are not solved. I am a old matlab user, I love matlab, however, it was a decision of my supervisor. The problem is simple: serial port errors in the batch cause the corruption of matlabprefs.mat.
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.
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

"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.
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 MATLAB Compiler SDK in Help Center and File Exchange

Products

Release

R2023a

Asked:

on 19 Aug 2024

Answered:

on 10 Sep 2024

Community Treasure Hunt

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

Start Hunting!