A little help with translating a piece of Fortran code to Matlab
조회 수: 3 (최근 30일)
이전 댓글 표시
I know this is bit of an odd question, and I'm sorry if this is against any community rules.
I'm troubleshooting a Fortran code and translating it to Matlab, but I have diffuclty with the following piece. I don't have a thorough understanding of Fortran. I can understand that the following code reads a data file line by line using the format string. I believe the Matlab equivalent would be fscanf with a formatSpec. But I cannot understand how to formulate it.
do j=1, nb_pan
k = min(12, 2*modes)
write(string,"('(1X, ES13.6, 1X, I2, 1X, I9, ', I2, '(1X, ES13.6))')") k
read(10, string, iostat=io) tmp2(1,j), dumi1, dumi2, tmp2(2:1+k, j)
if (io/=0) write(*,*) 'Read failure in: ', i, j, k
do
if (2*modes<=k) exit
kk = min(12, 2*modes-k)
write(string,"('(23X, ', I2, '(1X, ES13.6))')") kk
read(10,string, iostat=io) tmp2(1+k+1:1+k+kk, j)
if (io/=0) then
write(*,*) string
write(*,*) ' Read failure in: ', i, j, k, kk
write(*,*) tmp2(1+k+1-12:1+k+kk-12, j)
end if
k = k+kk
end do
end do
I have uploaded a sample data file too.
Again, I'm sorry for the odd question. I appreciate any help. TIA!
댓글 수: 1
James Tursa
2023년 6월 15일
편집: James Tursa
2023년 6월 15일
I can maybe find some time to look more at this later. But the first observation is the write statements into string are simply creating a format on the fly, where the k and kk are used as "repeat" indicators for the format. Then this string format is used to read a line from a file from unit 10. That's all that is going on with this code.
답변 (2개)
Image Analyst
2023년 6월 15일
Not sure if you're trying to read or write stuff, but will this work for you:
fileName = 'testtext.txt';
dataContents = importdata(fileName)
% Show results
dataContents.data
dataContents.textdata
You can parse the array further if you want to.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!