Question about using 'load' to load an .m file

I'm currently trying to use the load command to pull data from an existing .m file I've downloaded from the internet, but the .m file is coming in as a [0x0] vector, and it shouldn't be.
I'm getting the message
"Attempted to access sampleDataFlow(:,1); index out of bounds because size(sampleDataFlow)=[0,0]."
when I attempt to define a variable to column 1. Any idea why this would be happening?
Thanks

답변 (3개)

Ryan G
Ryan G 2012년 9월 14일

0 개 추천

Although you can use the load command with an m-file, in my humble opinion, this would not be the ideal way to transfer data nor read data.
My suggestion would be to utilize the Import Tool available in the Home tab (R2012b). It's available in other versions but I like to be forward thinking.
This will allow you to utilize a GUI for the first time you import making it easy to determine if the data was imported correctly and easy to change the import method to achieve desired results. Once you're established the correct method you will be able to automatically generate a script or function to do it again and again.
The link above describes it better than I ever could.

댓글 수: 9

Robert
Robert 2012년 9월 14일
I appreciate your answer, however for the purpose of my work I have to understand how to use the load command with the m-file, as it is what I will primarily be using in a current class.
Ryan G
Ryan G 2012년 9월 14일
편집: Ryan G 2012년 9월 14일
Yikes! Tell your instructor this doesn't make sense!
Are you sure it is an m-file? As in, it has a .m extension and you double click it you can read the contents and edit them like a txt file?
Robert
Robert 2012년 9월 14일
Yes, it is definitely an m-file online, but when I save it and open it in matlab using 'load', it comes in as [0x0]. I don't understand why.
What are the first couple lines inside the file?
Say I create a MATLAB file, and put the contents as
1 2 3
4 5 6
save it as myfile.m, then use the command
x = load('myfile.m')
You will see it loads the data into x and displays this in the command window. Try this out first and make sure it works. It should load it like an ascii file I believe. What does the m-file look like when you open it? What type of data is inside?
First couple of lines look like this. The 3 columns continue for a while though. It's basically just a large collection of data.
0.00 0.00 0.00
2.00 0.54 0.54
4.00 2.54 1.48
6.00 4.51 0.49
8.00 5.47 0.46
When I open the m-file, it is displayed as blank when I double click on it.
Ryan G
Ryan G 2012년 9월 14일
The two things you say are kind of opposing each other. On the one hand you say it contains the data on the other you say when you open it it's empty. How do you know what data it contains if it's empty when you open it?
Robert
Robert 2012년 9월 14일
I mean to say, the file online contains the data. When I import it however, the data disappears. I'm sorry I didn't make that clear.
Ryan G
Ryan G 2012년 9월 14일
I would verify that hte data exists in the downloaded file. It sounds like the issue is coming up between downloading the file and the file that exists locally
Robert
Robert 2012년 9월 14일
That's also what I think. I'm not sure what the issue would be. I'm using "save target as..." to do the download, and it hasn't ever failed me before, so I'm not sure what's going wrong. Frustrating.

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

Walter Roberson
Walter Roberson 2012년 9월 14일

0 개 추천

Try using the -ascii option when you load()
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 14일
편집: Azzi Abdelmalek 2012년 9월 14일

0 개 추천

you can get your data like you import any text file
fid = fopen('file.m');
line1 = fgetl(fid)
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);

카테고리

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

제품

태그

질문:

2012년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by