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!