Reading Specific numbers from a text file
Show older comments
Hello,
I'd like to read the point coordinates from the header of a text file and am struggling to figure out how to implement this. I've read some of the documentation on regularexpressions and textscan but haven't been able to get something that works. All of the text files are formatted with some variation of the following header, where the point coordinates I am interested in reading I have bolded and underlined.
% Model: GoldenGooseOriginal.mph
% Version: COMSOL 5.6.0.280
% Date: Apr 6 2021, 17:03
% Table: Point1A - Point 1A
% t (s) Concentration (mol/m^3), Point: (0.0049, 0.0075)Electric potential (V), Point: (0.0049, 0.0075)
1 0.684462676129974 -184.39849836571287
6 0.6837221107343137 -184.96623080885865
11 0.6824717208473429 -185.1598084465351
...
My code so far looks like the following, but it only reads the data below the headerlines.
AllFileNames = [{'Point1A.txt','Point1B.txt','Point1C.txt',...
'PointCenterA.txt','PointCenterB.txt','PointCenterC.txt',...
'Point2A.txt','Point2B.txt','Point2C.txt'}];
for i = 1:length(AllFileNames)
% Next we import a file
filename = char(AllFileNames(i));
fileID = fopen(filename);
Cdata = textscan(fileID,'%f32 %f32 %f32','HeaderLines',5);
% find the point coordinates: (the following was my attempt at trying
% to implement the following answer)
while ~feof(fileID)
st = fgetl(fileID);
if ~isempty(strfind(st,'Point: (')) % I'd like to read the point coordinates that immediately follow 'Point: ('
stread = textscan(st,'%f32 %f32');
F = [stread{2}(1)];
end
end
fclose(fileID);
end
I'm not sure this is the best method but is just something I found in a previous question here:
It seems like Cdata is reading to the end of the file, therefore causing my while loop to be skipped. Maybe reordering my code would fix this BUT something is not working in the while-loop such that it currently reads to the end of the file without noticing 'Point: ('.
Any suggestions on how to get these point coordinates read and the subsequent data?
Thank you!
Accepted Answer
More Answers (0)
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!