Reading Specific numbers from a text file
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Charles Dorchester
il 4 Mag 2021
Commentato: Charles Dorchester
il 7 Mag 2021
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!
0 Commenti
Risposta accettata
dpb
il 5 Mag 2021
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 re
...
You've moved past the point in the file of the data of interest for the coordinates by reading the rest of the data firsl...your code to read the file line-by-line is fine, it's just misplaced in that you need to read that first, then you could continue on to read the rest of the file, but you won't need (and won't want) any 'Headerlines' argument then; you'll have read all the preceding lines already.
You could just insert a rewind(fileID) but that's kinda' clunky...
for i = 1:length(AllFileNames)
fileID = fopen(AllFileNames{i});
while ~feof(fileID)
st = fgetl(fileID);
if isempty(strfind(st,'Point: ')), continue, end % skip the unwanted records
xy=str2double(split(extractBetween(extractBetween(l,'Point:','Electric'),'(',')'),','));
end
Cdata = textscan(fileID,'%f32 %f32 %f32','HeaderLines',5);
fclose(fileID);
% remember to do whatever with xy, Cdata here before going on to the next
% file...otherwise, will just overwrite and have only the last set in the
% end
...
end
Got rather clever in the use of the new-fangled string class functions, the above works as long as the word 'Electric' also immediately follows the wanted point set--if there's another variable possibly in one of the files then you'll need somewhat more robust pattern matching to locate the substring...but it isn't hard, I just used that as being convenient with the given string to isolate the one desired set of parentheses from the multiple ones would get if just extractAfter with 'Point: ' as the pattern.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!