Error loading matlabprefs.mat

Hallo,
there is an error:
MATLAB:load:notBinaryFile
Unable to read MAT-file /home/myUser/.matlab/R2021a/matlabprefs.mat. Not a binary MAT-file. Try load -ASCII to read as text.
sometimes (this is not reproducible) it pops up after rebooting Matlab:
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 /home/myUser/.matlab/R2021a/matlabprefs.mat. File might be corrupt.
> In hgrc (line 163)
In matlab.graphics.internal.initialize (line 15)
Some pages (e.g. https://www.mathworks.com/matlabcentral/answers/364986-matlab-error-graphic) recommend deleting matlabprefs.mat. However, if I do, the error pops up again.
I also deleted full ~/.matlab folder. The error was gone for a couple of hours. Now, it reappears.
Does anybody know a persistent solution?
Thanks

6 个评论

/home/myUser/ ... is that a local drive, or are you accessing a network drive, such as using NFS file system?
this is a local drive
thanks
e.g. after restart of Matlab:
>> app.iP.localIpAddr = '192.168.213.1';
rosinit(app.iP.localIpAddr, 'NodeName', '/MatlabInit')
The value of the ROS_IP environment variable, 192.168.213.1, will be used to set the advertised address for the ROS node.
Initializing global node /MatlabInit with NodeURI http://192.168.213.1:41687/
>> app.iP.rosNode = ros.Node('/Matlab');
The value of the ROS_MASTER_URI environment variable, http://192.168.213.1:11311, will be used to connect to the ROS master.
The value of the ROS_IP environment variable, 192.168.213.1, will be used to set the advertised address for the ROS node.
>> app.iP.rtl = rostopic("list");
>> app.iP.d = rosdevice(app.iP.localIpAddr,'myUser', 'password');
Error using load
Unable to read MAT-file /home/sl1car/.matlab/R2021a/matlabprefs.mat. File might be corrupt.
Error in prefutils (line 10)
[varargout{1:nargout}] = feval(varargin{:});
Error in ispref (line 39)
Preferences = prefutils('loadPrefs');
Error in ros.codertarget.internal.DeviceParameters/getPref (line 445)
if ispref(prefGroup, pref)
Error in ros.codertarget.internal.DeviceParameters/getParam (line 140)
ret = obj.getPref(obj.SSHPortPrefName);
Error in ros.codertarget.internal.DeviceParameters.getSSHPort (line 296)
sshPort = device.getParam('sshport');
Error in rosdevice (line 160)
obj.Port = deviceParams.getSSHPort;
>>
You look like you are using Linux. When you are getting the error, what shows up for
!file /home/myUser/.matlab/R2021a/matlabprefs.mat
file ~/.matlab/R2021a/matlabprefs.mat
~/.matlab/R2021a/matlabprefs.mat: Matlab v5 mat-file (little endian) version 0x0100
That looks okay at the moment. At a time that MATLAB is telling you the file is corrupt, could you attach the file here for us to look at?
(Note: I am not completely sure where MATLAB stores github keys, or login credentials to reach your Mathworks account, and it is plausible they might be stored in that file, so there is some degree of security risk; I do not know if they are stored in that file or not, or how they might be encoded.)

请先登录,再进行评论。

 采纳的回答

Jan
Jan 2021-6-19
编辑:Jan 2021-6-25
If removing the file helps for some time, a function is destroying the file. Then an approach is to scan, who is accessi ng the file. Shadow the builtin save() function by adding this in a folder ontop of the path:
function save(varargin)
file = varargin{1};
if contains(file, 'matlabprefs', 'IgnoreCase', true) % [EDITED, Typo fixed]
disp(dbstack)
end
str =
evalin('caller', ['builtin(''save''', strjoin(varargin{:}, ',')]);
end
Does this work?! The strange calling syntax of the save() command, which takes char vectors as input to access the contents of the corresponding variables, is hard.

8 个评论

Dear Jan,
following your erecommendation, I see a couple of warnings:
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In matlab.internal.getCode (line 22)
In mtree (line 118)
In matlab.unittest.internal.ui.toolstrip.getFileInfoForToolstrip (line 29)
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In rmimap.StorageMapper
In rmimap/StorageMapper/getInstance
In rmiml.hasReqDataFile
In rmiml.visibleInToolstrip
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In rmimap/StorageMapper/getDefaultStorageName
In rmimap/StorageMapper/getStorageFor
In rmiml.hasReqDataFile
In rmiml.visibleInToolstrip
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In rmimap/StorageMapper/getDefaultStorageName
In rmimap/StorageMapper/getStorageFor
In rmiml.hasReqDataFile
In rmiml.visibleInToolstrip
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In rmiml.hasReqDataFile
In rmiml.visibleInToolstrip
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In rmiml.hasReqDataFile
In rmiml.visibleInToolstrip
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In matlab.internal.getCode (line 22)
In mtree (line 118)
In matlab.unittest.internal.ui.toolstrip.getFileInfoForToolstrip (line 29)
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In dos (line 67)
In checkMyStatus>checkMyStatus (line 40)
In timer/timercb (line 111)
In timercb (line 24)
In ros.msggen.diagnostic_msgs/KeyValue/loadobj (line 49)
In ros.Message>@(x)feval([ft,'.loadobj'],x) (line 400)
In ros/Message/reload (line 400)
In ros/Message/fromStruct (line 223)
In ros/Message (line 73)
In ros.msggen.diagnostic_msgs.DiagnosticStatus
In ros.msggen.diagnostic_msgs/DiagnosticStatus/loadobj (line 83)
In ros.Message>@(x)feval([ft,'.loadobj'],x) (line 400)
In ros/Message/reload (line 400)
In ros/Message/fromStruct (line 223)
In ros/Message (line 73)
In ros.msggen.diagnostic_msgs/DiagnosticArray
In ros.internal/SubscriberCallbackHandler/processNewMessage (line 56)
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
> In dos (line 67)
In checkMyStatus>checkMyStatus (line 40)
In timer/timercb (line 111)
In timercb (line 24)
in checkMyStatus (line 40) I have a system call reading a log file:
[status,cmdout] = system('cat can0log.txt');
How may this line this cause the issue? It periodically reads a log file, which is constantly re-written by a shell script running in background.
Without parallel computing, these line never caused problems.
However, the checkMyStatus runs in the main thread, not inside a worker. I would like to run it inside a worker, but this function accesses multiple uiLamp colors using app.myLamp.Color
Thanks for your help!
The warnings are not useful, when you shadow save() on purpose. Get the warning identifyier using lastwarn and try to disable this warning using the warning('off', ...) command.
If you access a file from multiple threads at the same time, confusions are expected. But reading should not cause troubles.
What about making a backup of the preferences file and restore in in case of troubles?
For testing, all savings in the code are eliminated, now. I feel the only one who saves is Matlab now writing the matlabprefs.mat
Now, there is trouble with the strange syntax of your save command.
Thre is the error:
Error using myMonitor (line 24)
File: save.m Line: 3 Column: 53
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Error in myApp/myButtonValueChanged (line 355)
app.rP = monitor(app); % app.myButton.Value;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 453)
Error while evaluating StateButton PrivateValueChangedFcn.
Warning: Function save has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
The main error from above still happens:
Unable to read MAT-file ~/.matlab/R2021a/matlabprefs.mat. Not a binary MAT-file. Try load -ASCII to read as text.
Change
if contains(file, 'matlabprefs', 'IgnoreCase', true))
to
if contains(file, 'matlabprefs', 'IgnoreCase', true)
Is there a way preventing Matlab saving new preference?
I like my Matlab to start always in the one and the same setup. I do not like Matlab to write new preferences on the fly.
Is there a check to disable Matlab writing to matlabprefs.mat ?
Perhaps you could make the file read-only ?
@Andreas Schwager: This is the purpose of the code I've posted: Preventing SAVE to access the preferences file or to track, who is corrupting it.
Dear Jan,
yes, I do not know when or why, however Matlab requires me to validate the local PCT cluster. This fails, if save is prevented. So, in the long term, there must be any better solution.

请先登录,再进行评论。

更多回答(1 个)

LucasBar
LucasBar 2024-8-6
I have this matlabprefs.mat corrupted while I was using batch(). I had 5 sensors collecting data simultaneously, each job batch were reading information from different serial ports. Suddenly, this matlabprefs.mat was corrupted and one sensor stopped to save data. How I could deal with this kind of error. I can not reboot matlab or computer for that. I need to keep my processing running.

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by