Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reading from file
Hi everyone!
I was playing with some code from a tutorial and was a bit perplexed with the result. Can someone please explain what's going on? The following data are in a file: ------------------- This is some test data "x-values","y-values" 0.0,1.4 1.0,4.3 2.0,6.2 3.0,5.1 4.0,6.4 5.0,7.7 6.0,7.9 7.0,8.4 8.0,7.3 9.0,6.2 10.0,9.1 ======================================= 'Following code reads those data from the file Dim xvals(100) As Single Dim yvals(100) As Single Dim npts As Integer Sub GetDataFromDisk() Open ThisWorkbook.Path + "\Task2.2.dat" For Input As #1 Line Input #1, Title$ Cells(1, 1) = Title$ Input #1, xtit$, ytit$ Cells(2, 1) = xtit$ Cells(2, 2) = ytit$ npts = 0 While Not EOF(1) npts = npts + 1 Input #1, xvals(npts), yvals(npts) Cells(2 + npts, 1) = xvals(npts) Cells(2 + npts, 2) = yvals(npts) Wend Close #1 End Sub ======================== And this is what I got (look at the y-values): x-values y-values 0 1.39999997615814 1 4.30000019073486 2 6.19999980926513 3 5.09999990463256 4 6.40000009536743 5 7.69999980926513 6 7.90000009536743 7 8.39999961853027 8 7.30000019073486 9 6.19999980926513 10 9.10000038146972 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reading from file
Dim xvals(100) As Single
Dim yvals(100) As Single Dim npts As Integer ' Dimension variables used in the code, due to the dimensioning of ' xvals and yvals as array's with 101 items, this code will work for text ' files with up to 101 data points. Sub GetDataFromDisk() ' Open a text file named "Task2.2.dat" from the same directory as ' the current excel file is in... Open ThisWorkbook.Path + "\Task2.2.dat" For Input As #1 ' Get the first line (assumed to be a title) Line Input #1, Title$ ' Put the title in cell A1 Cells(1, 1) = Title$ ' Get the second line (assumed to be column headers) Input #1, xtit$, ytit$ ' Put the column headers in A2 and B2 Cells(2, 1) = xtit$ Cells(2, 2) = ytit$ ' Set the number of data points to 0 npts = 0 ' While their is still data in the file do . . . While Not EOF(1) ' Increment points counter npts = npts + 1 ' Get an x and y Input #1, xvals(npts), yvals(npts) ' Put them in cells A(2+counter) and B(2+counter) Cells(2 + npts, 1) = xvals(npts) Cells(2 + npts, 2) = yvals(npts) ' Loop Wend ' Close the open file Close #1 End Sub ======================== And this is what I got (look at the y-values): x-valuesy-values 0 1.39999997615814 1 4.30000019073486 2 6.19999980926513 3 5.09999990463256 4 6.40000009536743 5 7.69999980926513 6 7.90000009536743 7 8.39999961853027 8 7.30000019073486 9 6.19999980926513 10 9.10000038146972 Note: I've seen much better code than this! Dan E |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reading from file
Here's the original data:
"x-values","y-values" 0.0,1.4 1.0,4.3 2.0,6.2 3.0,5.1 4.0,6.4 5.0,7.7 6.0,7.9 7.0,8.4 8.0,7.3 9.0,6.2 10.0,9.1 And here's the output: x-values y-values 0 1.39999997615814 1 4.30000019073486 2 6.19999980926513 3 5.09999990463256 4 6.40000009536743 5 7.69999980926513 6 7.90000009536743 7 8.39999961853027 8 7.30000019073486 9 6.19999980926513 10 9.10000038146972 ------------------------------------- Dan E wrote in message ... Provide an example of the differences and I can try, though their are people on this board who know more about data types, etc... than me. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reading from file
The error is a result of assigning the values to single precision
data types. This topic has been discussed here previously, here are some threads on that subject http://www.google.com/groups?hl=en&l...cel%2Bsingle%2 Bprecision%2Berror%26start%3D30%26hl%3Den%26lr%3D% 26ie%3DUTF-8%26selm%3D3DF01491.8090500%2540no_e-mail.com%26rnum%3D34 http://www.google.com/groups?hl=en&l...rev=/&frame=on http://www.google.com/groups?hl=en&l...groups%3Fhl%3D en%26lr%3D%26ie%3DUTF-8%26selm%3D131ac1c1.0308060311.df78cc2%2540posting .google.com watch out for word wrap on the above links you may need to cut and paste them into your browser. Dan E "Asif" wrote in message ... Here's the original data: "x-values","y-values" 0.0,1.4 1.0,4.3 2.0,6.2 3.0,5.1 4.0,6.4 5.0,7.7 6.0,7.9 7.0,8.4 8.0,7.3 9.0,6.2 10.0,9.1 And here's the output: x-values y-values 0 1.39999997615814 1 4.30000019073486 2 6.19999980926513 3 5.09999990463256 4 6.40000009536743 5 7.69999980926513 6 7.90000009536743 7 8.39999961853027 8 7.30000019073486 9 6.19999980926513 10 9.10000038146972 ------------------------------------- Dan E wrote in message ... Provide an example of the differences and I can try, though their are people on this board who know more about data types, etc... than me. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Reading from file
Fix you clock - it isn't december yet.
-- Regards, Tom Ogilvy Asif wrote in message ... Thanks very much, Dan. You explained it very well. But can you explain why the y-values are slightly different from what they were originally (in the file)? Dan E wrote in message ... Dim xvals(100) As Single Dim yvals(100) As Single Dim npts As Integer ' Dimension variables used in the code, due to the dimensioning of ' xvals and yvals as array's with 101 items, this code will work for text ' files with up to 101 data points. Sub GetDataFromDisk() ' Open a text file named "Task2.2.dat" from the same directory as ' the current excel file is in... Open ThisWorkbook.Path + "\Task2.2.dat" For Input As #1 ' Get the first line (assumed to be a title) Line Input #1, Title$ ' Put the title in cell A1 Cells(1, 1) = Title$ ' Get the second line (assumed to be column headers) Input #1, xtit$, ytit$ ' Put the column headers in A2 and B2 Cells(2, 1) = xtit$ Cells(2, 2) = ytit$ ' Set the number of data points to 0 npts = 0 ' While their is still data in the file do . . . While Not EOF(1) ' Increment points counter npts = npts + 1 ' Get an x and y Input #1, xvals(npts), yvals(npts) ' Put them in cells A(2+counter) and B(2+counter) Cells(2 + npts, 1) = xvals(npts) Cells(2 + npts, 2) = yvals(npts) ' Loop Wend ' Close the open file Close #1 End Sub ======================== And this is what I got (look at the y-values): x-valuesy-values 0 1.39999997615814 1 4.30000019073486 2 6.19999980926513 3 5.09999990463256 4 6.40000009536743 5 7.69999980926513 6 7.90000009536743 7 8.39999961853027 8 7.30000019073486 9 6.19999980926513 10 9.10000038146972 Note: I've seen much better code than this! Dan E |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Reading from a text file | Excel Discussion (Misc queries) | |||
reading from another file and pasting to current file, "combobox" | New Users to Excel | |||
Reading a text file ? | Excel Worksheet Functions | |||
Reading a text file | Excel Discussion (Misc queries) | |||
Reading from a closed file. | Excel Worksheet Functions |