I am not sure how to even get started. I am supposed to use feof to open the file but I cannot get it to work. Can anyone show me how to use feof? Thanks

Before you start writing your program:
Read all of these instructions carefully.
Using previous assignments as a guide determine the following for this problem.
1. Problem Constants: (with units, as needed)
2. Problem Inputs: (with units, as needed)
3. Problem Outputs: (with units, as needed)
4. Other variables: (with units, as needed)
5. Equations
6. Algorithm: Copy and paste your algorithm as comments in your program.
Include 1-6 from above in the comments at the top of your Lab07.m file.
Program: Lab07.m
Edwin Hubble used the Mount Wilson Observatory telescopes to measure features of nebulae outside the Milky Way. He found that there is a relationship between a nebula’s distance from earth and the velocity with which it was traveling from the earth. Hubble’s initial data on 24 nebula is presented in Table 1 in the problem scenario.
The relationship between distance and velocity led scientists to propose that the universe came into being with a Big Bang, a long time ago. If material scattered from the point of the Big Bang traveling at a constant velocity, the distance traveled can be determined.
Using Hubble’s data, find the linear equation that estimates the relationship between the velocity and distance readings. Display the data in a table and graph.
General Instructions:
Insert comments at the top and throughout each file
o Include the follow comments at the beginning of this (and ALL) files.
your name Grade of zero for files with incorrect author name
assignment number Zero points for comments if no collaboration statement
date you completed the assignment
statement(s) about collaboration
a short narrative about what the file does
o Use the algorithm as comments throughout each file.
o Add section headers. See Standards for Documentation of MATLAB programs on Resources page on Canvas.
Variables:
o Use ALL CAPS for constants variable names.
o Start other variables with lower case.
o Use descriptive variable names.
o Use variables for data values. Note: zero(0), one (1), and column index numbers are allowed.
Code clarity:
o Indent blocks as needed. In editor, select ALL, right-click, Smart Indent in the pop-up menu.
o Divide you solution program code into sections as noted in the algorithm.
o Use section comments as well as the algorithm step comments.
o Use blank lines as needed to group statements.
Data file:
o Check for good open. Display system message and end program there is a problem.
o Use fscanf() to read one row at a time.
o Read Hubble’s data from hubbleData.txt. Read until the end-of-file is found
o You know that there are five columns, but you will need to count the rows of data read
o There are five columns of data in the data file. You will read all but only use the velocity and distance in the program computations. Compare hubbleData.txt with the table in the scenario for the column content.
o Velocity will be the independent variable(x) variable and distance dependent variable(y).
o You know that there are five columns, but you will need a row counter for the row index as the data is read.
Computation
o Use the given formula to compute the slope and intercept for the given data.
o Create the regression equation that estimates the relationship between the velocity and distance readings.
COMP1200M – Spring 2013 – Lab07 – p. 2 of 2
Output
o NO extra output, i.e. use semicolon as needed and use “clc, clear all” to remove previous output.
o Label output using the fprintf()function.
Format the output decimal places as shown in the sample below.
Include units, if applicable.
o Columns of numbers right-aligned.
o Print the velocities and distances in a two columns with a title and column headings
o Print slope and y-intercept in the form of a linear equation.
o Display the data in a scatter plot and line of the graph a linear equation
Use the code below to draw a scatter plot for the data pairs and draw a line of the linear equation
o Replace velocity column and distance column with matrix columns where you saved velocity and distance.
o “hold on” allows the line plot to be drawn in the same figure as the scatter.
o Create a vector xVelocity starting with minimum value of Hubble’s velocity and ending with the maximum value of Hubble’s velocity.
o Compute the values for yDistance using the linear equation that you created.
% create a scatter plot of the velocity and distance data
% plot the line created by your linear equation
scatter( velocity column, distance column )
hold on % allows both graphs in the same figure
xVelocity = _______________________________;
yDistance = _______________________________;
plot(xVelocity,yDistance)
Sample Input/Output:
NEBULA INPUT DATA
VELOCITY DISTANCE
km/sec 106 parsecs
170 0.032
290 0.034
-130 0.214
. . .
500 2.000
850 2.000
800 2.000
1090 2.000
LINEAR EQUATION: distance = 0.0014 * velocity + 0.399
Submit via Canvas:
Lab07.m MATLAB script file

 Accepted Answer

In the command window, type the following command:
doc feof
you will find there a very illustrative example. FEOF doesn't read the file actually; when a file is open, it allows you to test whether the cursor defining the position of the next character to read is at the end of the file or not.

15 Comments

I see what you mean. I guess the better question is how do I get the file open and check for good open? I know I am supposed to use fopen but am not sure how to use it. Thanks
See my comment below. As for FOPEN, try the following
>> fid = fopen('myFile.txt', 'r')
and see what value has fid in this case where the file probably doesn't exist as well as in a case where the file exists (create a text file with the notepad if needed, and write a few integers in it, separated by white-spaces). Note that the file must be in the current path/directory.
In the same time, read a bit the doc of FOPEN
>> doc fopen
so you understand what it means when fid is negative/positive.
So you have a file called hubbleData.txt or something similar? Did you try to open it with the notepad to see the structure of the content? If so, could you copy/paste the first 10 lines in a comment here?
Assuming you have hubbleData.txt, try to open it as indicate above, or even better:
[fid,msg] = fopen('hubbleData.txt', 'r')
see whether fid is positive or negative and what string is stored in msg. Now seeing this example, you should read the doc about FOPEN and start to understand a bit better how it works, e.g. the meaning/purpose of this 'r' that we pass as a second argument.
>> I have an undefined variable hubbleData, but I have it saved in my jump drive.
The file must be in the current path/directory in MATLAB. Pay attention to the fact that string constants are delimited by ''. So
hubbleData <- this is a variable name.
'hubbleData' <- this is a string.
Your file name should be a string, and the file is likely to have an extension, e.g. .dat, .txt. The forst argument of FOPEN must be a string (or a variable that contains a string) that is the file name (and path if needed) of the file to open.
It seems that you are working on the same problem as Ian barker by the way, who posted a question here a few hours ago.
Yes, I opened it with the notepad and I pasted the data under the other answer.
ok, well this means that you successfully opened the file. Now try to repeat the following command like 6 or 7 times and see what you get and how it corresponds to the file content that you pasted below:
fscanf(fid, '%f', 1)
You should see that each time you run this command, it reads the next "entry" in the file.. as if there were some cursor moving while you are reading. If you want to move the cursor back to the beginning of the file, you can do
frewind(fid) ;
If you want to close the file, you can do
fclose(fid)
After that you can re-open the file if you want, which puts the cursor at the beginning of the file again.
I'm leaving for the night, but at this point, I strongly advise you to read the doc of FOPEN, FCLOSE, FSCANF, and maybe go over small tutorials that cover MATLAB basics. The point is: if you post specific questions (which requires that you know at least the basics of MATLAB), e.g. I must read this file.. I wrote this code so far.. I get this error message, you'll get answers; but you are unlikely to get any answer if you post the full statement of your homework.
Ok, so I think that I have the code all the way up to the computations. I am not sure how to get the velocity and distance out of the data. The velocity is the third column and the distance is the second column. What function will draw out these columns? And how do I get them into a manageable vector? Is it something like this velocity=hubbleData(;,3); distance=hubbleData(;,2);
I caught that when Matlab was yelling at me lol. So now I have [fid,msg] = fopen('hubbleData.txt', 'r'); data = fscanf(fid, '%f', [24, 5]); fclose(fid) ; velocity=hubbleData(:,3); distance=hubbleData(:,2); ydistance = 0.0014 * velocity + 0.399; I do not know how to turn this into a graph of x velocity and y ydistance? How do I create a scatter plot graph?
[fid,msg] = fopen('hubbleData.txt', 'r');
data = fscanf(fid, '%f', [24, 5]);
fclose(fid) ;
velocity=hubbleData(:,3);
distance=hubbleData(:,2);
ydistance = 0.0014 * velocity + 0.399;
How do I crate a scatter plot graph using velocity as the x and ydistance as the y. Sorry about above comments, I just learned how to type in code.
[fid,msg] = fopen('hubbleData.txt', 'r');
data = fscanf(fid, '%f', [24, 5]);
fclose(fid) ;
velocity=hubbleData(:,3);
distance=hubbleData(:,2);
ydistance = 0.0014 * velocity + 0.399;
plot(velocity, distance) if true
% code
end
This creates a graph of the right points but is not a scatter plot. All I get is a worthless zigzag line. What is the problem?
[fid,msg] = fopen('hubbleData.txt', 'r');
data = fscanf(fid, '%f', [24, 5]);
fclose(fid) ;
velocity=hubbleData(:,3);
distance=hubbleData(:,2);
ydistance = 0.0014 * velocity + 0.399;
scatter(velocity, distance)
plot(velocity, ydistance)
This is what I have. Now I need to overlay these two functions. I do not know how to do this part?
data = fscanf(fid, '%f', [24, 5]);
here you store the output of FSCANF in variable data. This is what you should use below ..
velocity=hubbleData(:,3);
distance=hubbleData(:,2);
Your code might still be working if hubbleData is still defined from a previous test, but it won't work when you'll restart MATLAB or clear your workspace.
About HOLD, the reflex should be
>> doc hold
>> doc plot
and look at the examples. The one involving hold in the doc of PLOT for example can be copy/paste-d in the command window so you can play with the material.
Ok, this time I'm off for the night!

Sign in to comment.

More Answers (1)

have you gotten started yet? I have the same assignment and i have no idea where to start or what to do

6 Comments

I still dont know how to get started. What all do you have?
Well, you have a file with data; how does it look like? Copy/paste part of it as a comment here, caring for the formatting to be good enough so we can see the structure without having to spend time deciphering, and tell us what you tried to do so far for reading it.
filename= hubbleData; fileid,errormsg=(filename,'r')
This is what I have. Matlab keeps telling me I have an unbalanced input, right before the comma.
So, I changed the ( to a [ and it worked. Now it says that I have an undefined variable hubbleData, but I have it saved in my jump drive.
  • 1 .. 0.032 ..+170 .. 1.5 .. -16.0
  • 2 ..0.034 .. +290 .. 0.5 .. 17.2
  • 6822 ..0.214 ..-130 .. 9.0 .... 12.7
  • 598 ..0.263 ..-70 .. 7.0 .. 15.1
  • 221 ..0.275 ..-185 ..8.8 .. 13.4
  • 224 ..0.275 ..-220 ..5.0 .. 17.2
  • 5457 ..0.45 ..+200 .. 9.9 ..13.3
  • 4736 ..0.5 ..+290 .. 8.4 ..15.1
  • 5194 ..0.5 ..+270 .. 7.4 ..16.1
  • 4449 ..0.63 .. +200 .. 9.5 ..14.5
  • 4214 ...0.8 .. +300 .. 11.3 .. 13.2
  • 3031 ..0.9 ...-30 .. 8.3 .. 16.4
  • 3627 .. 0.9 ..+650 .. 9.1 .. 15.7
  • 4826 ..0.9 ..+150 .. 9.0 ..15.7
  • 5236 ..0.9 ..+500 .. 10.4 .. 14.4
  • 1068 ...1.0 .. +920 .. 9.1 ..15.9
  • 5055 ...1.1 ...+450 ... 9.6 .... 15.6
  • 7331 ...1.1 ...+500 ... 10.4 ... 14.8
  • 4258 ... 1.4 ...+500 ... 8.7 ...17.0
  • 4151 ... 1.7 ... +960 ...12.0 ... 14.2
  • 4382 ... 2.0 ... +500 ...10.0 ... 16.5
  • 4472 ... 2.0 ... +850 ... 8.8 ...17.7
  • 4486 ... 2.0 ... +800 ... 9.7 ...16.8
  • 4649 ... 2.0 ... +1090 ... 9.5 ... 17.0
I'll go on below my answer, further up in the page.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 20 Mar 2013

Community Treasure Hunt

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

Start Hunting!