Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Question

  1. Feb 27, 2008 #1
    Is there any code that i can use to execute a file through a fortran compiler? For example i want to execute an excel file (kk.xls) just before the termination of the main code.
    Thank you
  2. jcsd
  3. Feb 27, 2008 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You can't "execute" an Excel file. You might want to be more specific about what you mean.

    - Warren
  4. Feb 27, 2008 #3
    I want to write a code line that will call an excel file after running the main code. I just want to call the file! Not input or export any data. So just before the code terminates it will call this file on the screen. just that. although i dont know if it is possible.
  5. Feb 27, 2008 #4


    User Avatar
    Homework Helper

    Sounds like you want to run Excel with a file you created from your program. This could be done via a batch file that first runs your program and then runs Excel. If you want to run Excel from within your program via an internal command line interface, your Fortran compiler would have to support this. It would be nice if the Excell program could be launched as an independent task so your program could exit and not occupy space after it's done and the Excell program starts.
  6. Feb 28, 2008 #5
    Exactly Jeff! But how am i doing that?
  7. Feb 28, 2008 #6


    User Avatar
    Homework Helper

    What you want is the equivalent of ShellExecute() that is available in Microsoft C or C++ compilers. The alternative is to create a dos console batch file and then optionally create a shortcut to the batch file in a program folder.

    To create shortcuts:

    Click on start, right click on all programs. Create a new folder. Double click on the new folder to open it. Right click on folder window and select new, shortcut. Search for your program (the .exe) file. If the program has an icon in it, it will automatically get picked up. If there are parameters, add them after the name of the program when creating the shortcut. Click on next and change the name if you want.

    If you didn't get an icon for the shortcut, right click on the shortcut, and click on change icon, and again, fine the .ico file for your program. Windows will start off with the generic set of icons from some .DLL file. You can pick up icons from .DLL files, .EXE files, or .ICO files.

    Note that this same process can also be used to create shortcuts to .BAT file to run command scripts. For Windows XP or Vista batch files, prefxing a program path and name with start, such as

    start \myfolder\myprogram1.exe
    start \myfolder\myprogram2.exe

    allows multiple programs or multiple instances of a program to be launched with a single shortcut to a batch file.
  8. Feb 28, 2008 #7
    buy saing program you mean the file that contains the fortran code or the excel file? Because non of them is a .exe file. And then still what is the code to call the file from the fortran compiler? I want to call my excel file from the fortran code not make a script that will call both the fortran compiler to open and the excel file as well.
    Last edited: Feb 28, 2008
  9. Feb 29, 2008 #8


    User Avatar
    Homework Helper

    I don't know about the Fortran compiler. Using the ShellExecute() function in C / C++, you could choose to "open" or "edit" the file, which would autolaunch Excel if the file had an "Excel" associated suffix, such as SAMPLE.XLS. You could also to choose to "open" Excel.EXE with your created SAMPLE.XLS file as a parameter.
  10. Feb 29, 2008 #9
    1) Is there any code that can make a fortran compiler to export data to an excel file?
    But not just OPEN(3,FILE='Z2.xls') for example because it is inserting the data in a messed up way and i can not draw a graph.

    2)Also another in fortran code lets say we ve got OPEN(2,FILE='kk1.DAT') The part of the code in
    ' ' is understood by fortran as the name of the file that will open. Can i make 1 to be a variable so if i create a loop the name of the file will take the value of the variable. For example OPEN(2,FILE='kkX.DAT') where X will be the variable.
    Last edited: Feb 29, 2008
  11. Feb 29, 2008 #10


    User Avatar
    Homework Helper

    Output a text file SAMPLE.TXT, then open this with Excel.
    Use spaces, or commas to seperate the numbers on each line of the text file.
  12. Feb 29, 2008 #11
    Create/open a file as a text file, not .xls. Then when you write the data cells, insert tabs between each cell when you want to create new columns, or insert crlf if you want to make a new row. Then run the text file but open using using Excel. Specify tab separation between the cells.

    Yes you can do this. You will have to create the file name as a string variable by concatenating various components. First convert the number variable to an ascii character string, then concatenate 'kk' + 'numberVariable + '.DAT' and call it something else. Then OPEN(2,FILE='somethingElse')

    BTW I do not know any Fortran compilers that will directly call up an run an .exe program such as with ShellExecute() in C++. If you do this in C you will likely have to call "Excel.exe mySpreadsheet.xls". You may also have to call the Excel .exe and or the spreadsheet name as a full file pathname, e.g. "C:\blah\blah\mySpreadsheet.xls"
  13. Mar 4, 2008 #12
    Thank you for your answer. I do not know how to create a string variable or an ascii character or how to concaternate the variables together :S Can you be a little more detailed on that. I realised that fortran does not give to many options in plotting and i am desperatly trying to convert my code into matlab. I have used many converters. I managed to convert from fortran 77 to fortran 90 but i fail to do so on Matlab :frown:
  14. Mar 5, 2008 #13
    Perhaps this may help to open some doors:

    Code (Text):

          CHARACTER s*4        ! create string variabel 4 characters long
          DO 10 i=1,5
            WRITE(s,20) i      ! convert integer i to string in s using format in 20
            PRINT *, 'kk'//s//'.dat' ! concatenates 'kk' plus s plus '.dat'
       10 CONTINUE
       20 FORMAT(I4.4)
    This should print a list of file name that have an incrementing number in it, i.e.

    I must admit to being a bit lost as to you actual problem however.
  15. Mar 5, 2008 #14
    Thanks again vor your answer! It was helpful. The thing is that i have a code in fortran 77 that is giving me some data on external files every time i run it. It is a simulation of a flow. At first i was trying to create additional code so every time the program will give me a new notepad file with external data then the target file that contains the initial data will change its name into the name of the external data file. Then the program would run again but with different initial data ( as now the initial data will be the results data from the first run of the program) and would give me a different external data. And i wanted this to go on for 10 times in order to get 10 files with external data. But then i decided that instead of using excel to plot my data i could translate into matlab and do it a lot easier. So i managed to convert the code from fortran 77 to fortran 90 but i can not manage to translate into matlab
    as i am using f2matlab converter.
  16. Feb 21, 2011 #15
    I want to calculate sum of square of two matrix in fortran . how can I do that using " sum function".
  17. Feb 21, 2011 #16
    Hi dear
    i want to write a " if command" whit fortran which is too long.

    could you please what can I do?

    thanks a lot
  18. Feb 21, 2011 #17
    Hi dear
    i want to write a " if command" whit fortran which is too long.

    could you please what can I do?

    thanks a lot
  19. Feb 21, 2011 #18


    Staff: Mentor

    You need to be more specific about what you are trying to do.

    Also, instead of tacking your question onto the end of a thread that is almost three years old, it's better to start a new thread.
  20. Feb 21, 2011 #19
    Hi Mark44
    I didn't know what do you mean?

    could you please explain more clearly.

  21. Feb 21, 2011 #20


    Staff: Mentor

    Could you explain more clearly what you are asking for?
    I have no idea what you are asking.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Fortran Question
  1. Question - Fortran (Replies: 2)

  2. FORTRAN Question (Replies: 4)

  3. Fortran question (Replies: 2)

  4. Fortran Question (Replies: 32)

  5. Fortran Question SOS (Replies: 26)