Extract certain numbers from a text file.

3 views (last 30 days)
Ankita Arora
Ankita Arora on 24 Jun 2022
Answered: GandaBerunda on 24 Jun 2022
Hello all,
I have a text file with a bunch of data, from which I have to extract the data for some specific values e.g. in example text file, I want to extract the power corresponding to 90000000000 Hz for all existing values. I am struggling to read the specific data using matlab. Plz help to write a matlab code to find out the specific row and extract the corresponding data.
Thanks

Answers (2)

Karim
Karim on 24 Jun 2022
see below for one method to do this
% read the file
MyData = readmatrix('example_txt_file.txt');
% specify frequency value of intrest
Fi = 90000000000;
% look for the specfic value in the first column
Fi_loc = MyData(:,1) == Fi;
% get the corresponding value from the secnd column
Fi_val = MyData(Fi_loc,2)
Fi_val = 21×1
-42.3780 -41.5809 -40.8609 -40.1618 -39.5092 -38.8737 -38.2632 -37.7056 -37.1746 -36.6735

GandaBerunda
GandaBerunda on 24 Jun 2022
Hi Ankita,
You can use the readtable() function to read from the textfile into a table. After that, you can find out the rows which have the desired frequency using the equality operator, which will give you a logical array with 1's at the rows where desired frequency is present.By this stage, you would have the specific rows where the desired data is located and you can use the same to access the corresponding power.
Hope this helps.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!