A little help with translating a piece of Fortran code to Matlab
3 views (last 30 days)
Show older comments
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 Comment
James Tursa
on 15 Jun 2023
Edited: James Tursa
on 15 Jun 2023
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.
Answers (2)
Image Analyst
on 15 Jun 2023
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 Comments
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!