MATLAB Answers


Bug about saving a variable

Asked by Isaac
on 19 Dec 2011

Hello everybody,

i am experiencing a very strange bug in saving a variable.

Supposing variable name is "xxxxx"

if i execute the instruction

save filetarget xxxxx (which should save the xxxxx variable in the filetarget.mat file)

and then i execute

load filetarget

i receive the following error message: Error using ==>load Unable to read MAT-file C:\....filetarget.mat File may be corrupt

but if i create a variable with a shorter name, that is


and then i execute save filetarget x and then load filetarget

everything works...

what can it be??

Thanks in advice!!!!


It works fine on my system. Are your variables really only 5 characters long?

Could you show us the actual variable name you are having problems with?

Which MATLAB version are you using, and which MS Windows version?



No products are associated with this question.

3 Answers

Answer by Naz
on 19 Dec 2011

yourfile='name.mat'; %saves in the current directory  
save yourfile x
load yourfile

Strange... I don't get an error when using variable xxxxx. Maybe matlab has an issue with the path where you want to save it? Try saving it in the current directory first.

Try using this set:

save(yourfile, 'xxxxx');

where the 'new' variable will be a struct containing your variable xxxxx. To access it, you need this reference:



What? No, that would save to yourfile.mat not to name.mat !
save(yourfile, 'x') would be the code.

on 19 Dec 2011

In the code above, the variable 'x' is in the file 'name'.

I'm pretty sure that is not true, Naz.

save yourfile x

is taking advantage of command / function duality such as is described briefly by Steve at

save yourfile x

would be treated as


which would save to the file named yourfile.mat

If you examine the save() documentation at you will see that in each case where command/function duality is used, the filename given is treated literally rather than being evaluated. For example "save test.mat" does not attempt to extract the field named "mat" from the structure variable "test" and use the value of the field as the file name.

Answer by Jose Jeremias Caballero on 19 Dec 2011


    >> xxxxxxx=rand(3);
    >> save filetarget xxxxxxx
    >> clear all
    >> whos
    >> load filetarget
    >> whos
      Name         Size            Bytes  Class     Attributes
      xxxxxxx      3x3                72  double              
    >> display(xxxxxxx)
    xxxxxxx =
        0.4898    0.7094    0.6797
        0.4456    0.7547    0.6551
        0.6463    0.2760    0.1626


Answer by Isaac
on 20 Dec 2011

I explained myself in a wrong way. The problem is not "usual": saving and loaind usually works well with me, whatever is the name and its length of the variable.

The problem is about one specific variabile... which i dunno why if i save it and then load it .... it tells me that error message "Error using ==>load Unable to read MAT-file C:\....filetarget.mat File may be corrupt" but, more surprisingly, if i save it with another name... it works!!!!

that is:

variable "var1"

save filetarget var1 load filetarget ERROR


var2=var1 save filetaget var2 load filetarget

Everything works....



on 20 Dec 2011

the name is "rendimentoSTOXX600"

but i think it should be something else than name... because the same problems is given by another variable which is calculated from "rendimentoSTOXX600"

Jan Simon
on 20 Dec 2011

Please post running code and format it as explained in the "Markup help"link on this page.

Jan Simon
on 20 Dec 2011

Are you sure, that this is not working:
var1 = rand(10);
save filetarget var1
Data = load('filetarget.mat');

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!