Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

fprintf in a DATA *.dat format

Asked by MM on 21 May 2013

I use matlab to format a 4 columns dataset into a ascii file which is in turn the input for a fortran routine (that i cannot modify or see the source).

I found that the following code does produce the good array of characters (spaces, numeric, text) but i need to open the txt file with a text editor afterwards and "save as" "DATA *.dat" to make it suitable for my fortran routine.

Is there a way to do that within my matlab code ?

2 Comments

Matt Kindig on 21 May 2013

I'm not exactly sure what your question is. What is not working?

MM on 21 May 2013

I d like to avoid having to open the file afterwards with a text editor to transform to the appropriate encoding

i need to open the txt file with a text editor afterwards and "save as" "DATA *.dat" to make it suitable for my fortran routine.

by the way i m on win7

MM

Products

No products are associated with this question.

2 Answers

Answer by per isakson on 21 May 2013
Edited by per isakson on 21 May 2013

What happens if you just change

    namefile='nameout.txt';

to

    namefile='nameout.dat';

What encoding does the fortran-program require?

Doc on fopen says:

    encoding
    String that specifies the character encoding scheme to use for subsequent
    read and write operations, including fscanf, fprintf, fgetl, fgets,
    fread, and fwrite.

4 Comments

per isakson on 21 May 2013

You write "with a text editor" . Any editor? Does the fortran-program give any useful information in the error message?

MM on 21 May 2013

i used winedt

the fortran error isn't explicit about the encoding

per isakson on 21 May 2013

I don't know

  • is matlab and the fortan program on the same computer and operating system?
  • can you find out the encoding from the winedt help?
  • we don't really know what winedt does to the file?
  • can winedt be called by matlab? dos('winedt something' )
  • just take a chance and experiment with some different encodings fopen( .. )
per isakson
Answer by Walter Roberson on 21 May 2013

There is no particular format for .dat files; ".dat" is a suffix used to mean any binary format (and probably not one of the standardized ones.)

What you might perhaps be looking for is to use Fortran's unformatted read. If that is the case, then write the data out using fwrite()

0 Comments

Walter Roberson

Contact us