How to search and extract specific string and data and write into new variable from file ?
Show older comments
I have data containes lot of different values like this
BULK VARIABLES NW= 0.197E+10 QW= 0.302E-03 QWA= 0.931E-07
QSO2= 0.000E+00 QO3= 0.000E+00 QH2O2= 0.000E+00 QCO2= 0.000E+00 QNH3= 0.000E+00 QSO4 = 0.000E+00 QHMO3= 0.000E+00 pH= 0.594E+01 CONDUCT= 0.239E-01
And I would like to search pH= ****** at every line and extract from the file and write into a new variable (nx1).
you can find sample file in attached file.
Accepted Answer
More Answers (1)
Anmol Dhiman
on 10 Sep 2020
Hi Jeevan,
You can follow the below approach
fid = fopen('pH.txt'); % Open the file
line_ex = fgetl(fid); % read a line
newStr = split(line_ex); % split the line based on delimeter space.IT gives a cell array
indexOfPH = find(contains(newStr, 'pH=')); % search if any of the elements is mathing with 'pH='
newVariable = newStr{indexOfPH+1} ; % store the value in a variable
% To read next line
line_ex = fgetl(fid);
You need to write in a loop.
Regards,
Anmol Dmiman
1 Comment
Jeevan Kumar Bodaballa
on 26 Sep 2020
Edited: Jeevan Kumar Bodaballa
on 26 Sep 2020
Categories
Find more on MATLAB 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!