Need to read some values from a text file

I have a text file and I need to extract some values from the text file.
From line number 524 to 533, the Mode number and Eigen value need to be read and saved in a variable. Any help is appriciated.
Best Regard.

Answers (1)

This process consists of multiple steps:
  • read the file to a cellstr or to a string vector (you can use readlines on newer releases, and either regexp(fileread(___),'\n','split') or my readfile function, which you can get here)
  • find where your data begins
  • cut that section
  • convert that to numbers
txt=readlines('https://www.mathworks.com/matlabcentral/answers/uploaded_files/824870/buck.txt');
txt=cellstr(txt);
start_ind=3+find(contains(txt,"MODE NO EIGENVALUE"))
start_ind = 524
end_ind=find(cellfun('isempty',txt));end_ind(end_ind<=start_ind)=[];end_ind=end_ind(1)-1
end_ind = 533
txt(start_ind:end_ind)
ans = 10×1 cell array
{' 1 5.28896E+06'} {' 2 5.28897E+06'} {' 3 5.29998E+06'} {' 4 5.30000E+06'} {' 5 5.31633E+06'} {' 6 5.31644E+06'} {' 7 5.33461E+06'} {' 8 5.33466E+06'} {' 9 5.36301E+06'} {' 10 5.36314E+06'}
Now you can use tools like split, textscan, etc to convert these to double, which I will leave as an exercise to you. Feel free to comment with your try if you have trouble implementing that.

1 Comment

About that legacy syntax for cellfun: that syntax is the only place where cellfun is faster than an ordinary loop, but you do have to be carefull.

Sign in to comment.

Categories

Commented:

Rik
on 6 Dec 2021

Community Treasure Hunt

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

Start Hunting!