Error Reference to a cleared variable

Hi I am trying to load a .mat file that has a matrix array of 2500 rows x 75 columns, and then create a new file name x that has values from all rows and columns 1 to 66. This is my code:
load myfile.mat
x = myfile(:,:);
x=x(:,1:66);
I then get an Error that says Reference to cleared variable myfile. I used this same code with a .txt file and it worked, but have been stuck trying to figure out for hours why it wouldnt work for a .mat file. Any advice would be huge help for me.

댓글 수: 3

Is the matrix array in myfile.mat called myfile? Also, no need for the colons.
Use
x = myfile;
instead of
x = myfile(:,:);
meechellevdm
meechellevdm 2016년 10월 5일
Yeah, the mat file is called (i.e.saved as) myfile. Removing colons and brackets after x = myfile(:,:); gives me a different error: Undefined function or variable 'myfile'.
I get the same error if I add .mat to the command x=myfile.mat;
meechellevdm
meechellevdm 2016년 10월 5일
Hey I figured it out! Turns out the matrix inside the myfile was called something else which I had to specify during these commands
load ('myfile.mat', 'othernamematrix')
x = othernamematrix;
Yay, thanks Matthew you put me on the right track.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2016년 10월 5일

6 개 추천

If this is in a function and you use clear all or clear myfile inside that function, remove the clear all and/or clear myfile calls.
If it's inside a function but you don't call clear all please post a simplified version of that function with which you can reproduce the error.

댓글 수: 2

meechellevdm
meechellevdm 2016년 10월 5일
There was a clearall command inside the function (but before the commands: load myfile.matt
x = myfile(:,:);
x=x(:,1:66);
Nevetheless, even removing the clear all command doesnt make the error go away. I literally typed load myfile in the command window, and then x = myfile(:,:); after which I get the Error. I attach the myfile.mat.
meechellevdm
meechellevdm 2016년 10월 5일
ps that second t in myfile.mat was a typo and not how put it in the command window.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2016년 10월 5일

댓글:

2016년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by