AGilchrist
AGilchrist

Reputation: 13

How to read output from Excel file into Fortran?

I have an excel sheet with the following columns for a stock chart:

  1. Open
  2. High
  3. Low
  4. Close
  5. Day Average

How do i use Fortran to pull only the "Day Average" from the excel file?

I am new to Fortran and haven't been able to find anything that could help except for the link below but its pretty difficult for me to grasp since i am looking for something different than what the link is showing me:

http://en.wikibooks.org/wiki/Fortran/Fortran_simple_input_and_output

Upvotes: 1

Views: 12892

Answers (3)

Rook
Rook

Reputation: 62538

No, contrary to the other answers CSV is not the easiest file to read. Go to File/Save as/Other Formats and save it as Formatted text (space delimited). Depending on your locale, you will either have a comma or a full stop as a decimal point, so you'll have to (either use an external editor to do a simple search/replace) or write a fortran subroutine that goes character by character, and replaces every comma with a full stop.

After that it's easy, no ;'s to parse, so you just

program FreeFormat

real(4), dimension(5) :: open, high, low, close, dayaverage
real(4) :: average

open(unit=1, file='filename.prn', status='old')
do i=1,5
    read(1,*)open(i), high(i), low(i), close(i), dayaverage(i)
enddo
average = sum(dayaverage)/5
write(*,'("Average is",f5.2)')average

end program FreeFormat

You get the point ...


Here are a few links to get you started (Excel/Fortran DLL related) ...

Trouble with file location in excel/fortran dll connection
Fortran DLL & MS Excel

Upvotes: 8

M. S. B.
M. S. B.

Reputation: 29381

The native binary format of an Excel file will be very difficult to parse. Export the file to text or CSV, as already suggested. CSV will probably be easiest. You probably want to use "list directed IO", which has the source form:

read (unit-number, *) format-items

Fortran list-directed IO will read into the variables in the list "format-items" is a very flexible manner. The items in the file should be separated by deliminators such as spaces or commas. For your case, have five variables corresponding to the five columns in order to reach the 5th one that you want. Fortran is record-oriented, so you do one read per line.

Upvotes: 2

Wesley
Wesley

Reputation: 713

You'll have to read and parse the Excel file in Fortran to get the values you want. If you are new to the language then this might be very hard to do. Maybe it's easier to save the Excel sheet in a CSV format and parse that? Good luck!

Upvotes: 0

Related Questions