Find the value at exact location in a text file
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Mathieu NOE
2021년 3월 29일
hi
maybe you should share your data file and explain which data you're looking for
답변 (3개)
Mathieu NOE
2021년 3월 30일
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
per isakson
2021년 3월 30일
편집: per isakson
2021년 3월 31일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!