Find the value at exact location in a text file
3 views (last 30 days)
Show older comments
Hello,
I have a huge text file which has coordinates and the pressure values for each location. I need to find the pressure values for exact locations. Is anyone know how to do it?
Any and all help would be greatly appreiciated. Thank you!
2 Comments
Mathieu NOE
on 29 Mar 2021
hi
maybe you should share your data file and explain which data you're looking for
Answers (3)
Mathieu NOE
on 30 Mar 2021
hello
hope this helps
I used the attached function to load the data (FYI)
% demo
clc
clearvars;
% load data
[outdata,head] = readclm('data_ya.txt',4,1);
%
x_target = -3.1500000E-01;
y_target = 4.9999999E-03;
z_target = 9.4999999E-02;
tol = eps;
ind_x= find(abs(outdata(:,1) - x_target)< tol);
ind_y= find(abs(outdata(:,2) - y_target)< tol);
ind_z= find(abs(outdata(:,3) - z_target)< tol);
% find common single unique value accross ind_x / ind_y / ind_z
indxy_intersect = intersect(ind_x,ind_y);
indxz_intersect = intersect(ind_x,ind_z);
indxyz_intersect = intersect(indxy_intersect,indxz_intersect);
result_P = outdata(indxyz_intersect,4);
0 Comments
per isakson
on 30 Mar 2021
Edited: per isakson
on 31 Mar 2021
An alternative construct based on ismembertol(). I believe it's faster.
%%
fid = fopen( 'cssm.txt', 'rt' ); % cssm.txt contains the data provided in the question
cac = textscan( fid, '%f%f%f%f', 'Headerlines',1, 'CollectOutput',true );
[~] = fclose( fid );
num = cac{1};
%%
ism = ismembertol( num(:,1:3) ...
, [-3.1500000E-01,4.9999999E-03,9.4999999E-02] ...
, 1 ...
, 'DataScale' , [1e-8,1e-10,1e-9] ...
, 'ByRows' , true );
format long
pressure = num( ism, 4 )
outputs
pressure =
1.225449800000000e+05
>>
Example: [LIA,LocB] = ismembertol(A,B,'ByRows',true,'DataScale',[eps(1) eps(10) eps(100)])
The values of 'DataScale' together with the default value of tol, 1e-12, doesn't make sense to me. That's why I explicitely set tol = 1.
In response to comment
"I have 69 points" ismembertol() handles multiple "points" nicely. Replace the second section of my script by (I just copy&pasted the two rows following your bold row.)
%%
ism = ismembertol( num(:,1:3) ...
, [ -3.1500000E-01,4.9999999E-03,9.4999999E-02
-3.0500001E-01,4.9999999E-03,9.4999999E-02
-2.9500002E-01,4.9999999E-03,9.4999999E-02 ] ...
, 1 ...
, 'DataScale' , [1e-8,1e-10,1e-9] ...
, 'ByRows' , true );
format long
locations = find( ism )
pressure = num( ism, 4 )
and you get
locations =
69
70
71
pressure =
1.0e+05 *
1.225449800000000
1.225449800000000
1.225449900000000
>>
Are all 2,800,000 locations unique?
0 Comments
See Also
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!