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
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 个)

Les Beckham
Les Beckham 2023-6-15
编辑:Les Beckham 2023-6-15
The easiest way to read a file line by line in Matlab is with the readlines function.
Example:
str = readlines('test.txt')
str = 11×1 string array
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." "" "Aliquam sodales, lacus a sollicitudin maximus, nunc sem commodo libero, a consequat nisi odio at mi." "Vestibulum tristique augue consequat tellus auctor lobortis. Pellentesque a tellus bibendum lorem" "tristique commodo sed nec tortor. Proin tempor luctus nisl, nec tempor nibh congue a." "" "In interdum nec purus sed scelerisque. Duis pulvinar purus mi, vel commodo est malesuada sed." "Nulla condimentum erat nec velit sollicitudin, sit amet fermentum erat blandit." "" "Sed vitae gravida metus, mattis volutpat nisi. Aenean id nisl sit amet neque vestibulum eleifend." ""
  2 个评论
Jake
Jake 2023-6-15
Thank you, @Les Beckham. But the data I have are numeric values, and this gives a set of strings. Maybe I'm missing some trivial part, but I don't think using a function such as str2num would work.

请先登录,再进行评论。


Image Analyst
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)
dataContents = struct with fields:
data: [44×15 double] textdata: {'Numeric Output -- Filename '}
% Show results
dataContents.data
ans = 44×15
-1.0000 1.0000 1.0000 0.0303 0 0.0446 0 0.1337 0 0.0016 0 0.0647 0 -0.0461 0 0 0 0 0 0 0 0 0 0 0 0 0 NaN NaN NaN 0 0 0 0 0 0 0 0 -0.0242 0 -0.1634 0 NaN NaN NaN 0.0201 0 -0.1579 0 0.0530 0 -0.1604 0 0.0544 0 -0.1660 0 NaN NaN NaN 0.0242 0 -0.1634 0 0.0266 0 -0.3212 0 0.0835 0 -0.2905 0 NaN NaN NaN 0.1032 0 -0.3208 0 0.0486 0 -0.3519 0 -0.0266 0 -0.3212 0 NaN NaN NaN 0.1963 0 -0.3585 0 0.2025 0 -0.3321 0 0.1479 0 -0.4797 0 NaN NaN NaN -0.0874 0 -0.5047 0 -0.1963 0 -0.3585 0 0.3322 0 -0.0876 0 NaN NaN NaN 0.3027 0 -0.1950 0 0.1848 0 -0.6337 0 -0.3504 0 -0.4466 0 NaN NaN NaN -0.3322 0 -0.0876 0 0.0770 0 0.2947 0 0.2204 0 0.0892 0 NaN NaN NaN
dataContents.textdata
ans = 1×1 cell array
{'Numeric Output -- Filename '}
You can parse the array further if you want to.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by