필터 지우기
필터 지우기

How to read fortran formatted data from a text file? (for example: 0.556802469135838D+00)

조회 수: 1 (최근 30일)
I got a file with contents like follow:
  • 0.139200617284040D+00
  • 0.139200617284039D+00
  • 0.556802469135838D+00
  • 0.348001543210539D-01
I'd used matlab function like fscanf/textscan to read the file, while I failed. Beacuse the data use "D" rather than "E" to indicate double type, as fortran does.
Is there anyway that this problem could be fixed?

답변 (1개)

Geoff Hayes
Geoff Hayes 2015년 2월 1일
Alexander - why not read in the data as strings, replace the D with an E using either strrep or regexp, and then convert the string to a double using str2num? An alternative would be to open the fire in a text editor and replace all occurrences of D with E and then load the file as before.
  댓글 수: 1
Alexander H
Alexander H 2015년 2월 2일
Thanks, Geoff.
The first solution entered my brain is to replace all "D" by a text editor ultraedit. But it is not convenient for too much files. Then I have been using a temporary method:
tmp= fscanf(fid,'%fD%f',2); Data(ii)= tmp(1)*(10^tmp(2));
Sure you provided a workable technique, and your method is much concise and consists matlab style. While, using string method is much slower than directly read. Redundant MUL is also not so welcome. I wonder if matlab provided such a built-in function or input format but we don't know yet?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fortran with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by