Need to read some values from a text file
Show older comments
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"))
end_ind=find(cellfun('isempty',txt));end_ind(end_ind<=start_ind)=[];end_ind=end_ind(1)-1
txt(start_ind:end_ind)
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
Rik
on 6 Dec 2021
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.
Categories
Find more on Characters and Strings 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!