# filtering data inside a table and storing multiple tables in one big table

조회 수: 2(최근 30일)
Salma fathi 2022년 7월 20일
댓글: Mathieu NOE 2022년 8월 22일
i am trying to do the following
After reading some data files where each file contains data stored in a table.
1. we find the maximum point for one of the variables (in the 6th column in the table) according to the following criterea.
for ied=1:length(EDP)
imax=[];
[~,imax] = max(EDP(:,6));
if EDP(imax,1) < 190 % the first column in the table is the height
EDP(imax,:)=[];
elseif EDP(imax,1) > 190
2.then we check the following two conditions for this maximum point:
• the latitude for this max value should be in the interval (-12-1.5,-12+1.5)
• the longitude for this max value should be in the interval (-76.8-3,-76.8+3)
where the latitude and longitude are the 2nd and 3rd column in the table. If these conditions are satisfied then we store all the tables that have the condition satisfied in one big table, if the conditions are not satisfied then we simply ignore this table and go to the next file to read
To do that I have tried the following using two if statement inside the above for loop but I am not sure if I am doing this corectly or if there is a more easier way in matlab other than an if statement
if EDP(imax,2)>(stnLat-1.5) && EDP(imax,2)<(stnLat+1.5)
fprintf('If statement is passed.\n')
if EDP(imax,3)>(StnLong-3) && EDP(imax,3)<(StnLong+3)
fprintf('If statement is again passed.\n')
fprintf('\n')
if ifile==3
EDPAll=EDP(:,:);
else
EDPAll=[EDPAll;EDP(:,:)];
end
end
else
fprintf('If statement is NOT passed.\n')
end
If anyone can help, thank you in advance

댓글을 달려면 로그인하십시오.

### 채택된 답변

Mathieu NOE 2022년 7월 20일
hello
you can probably make the all process much simpler with only one main for loop (iteration over the files) and one if condition statement based on which condition must be met to store one's file data
this , I think, reflects the idea in a (hopefully) more straigthforward and cleaner code :
clc
clearvars
% init
stnLat = -12;
StnLong = -76.8;
EDPAll=[];
nFiles = 3; % dummy number of files (to get the principle)
for ck = 1:nFiles
load('EDP.mat');% contains : EDP1 EDP2 EDP3 EDP4 EDP5 EDP6 Date
EDP = table2array(EDPT(:,1:6));
% After reading some data files where each file contains data stored in a table.
% 1/ we find the maximum point for one of the variables (in the 6th column in the table) according to the following criterea.
[~,imax] = max(EDP(:,6)); % no need to loop here
% 2/ then we check the following two conditions for this maximum point:
% the latitude for this max value should be in the interval (-12-1.5,-12+1.5)
% the longitude for this max value should be in the interval (-76.8-3,-76.8+3)
% the latitude and longitude are the 2nd and 3rd column in the table.
% If these conditions are satisfied then we store all the tables that have the condition satisfied in one big table,
% if the conditions are not satisfied then we simply ignore this table and go to the next file to read
cond1 = (EDP(imax,1) > 190); % the first column in the table is the height
cond2 = EDP(imax,2)>(stnLat-1.5) && EDP(imax,2)<(stnLat+1.5); % the latitude for this max value should be in the interval (-12-1.5,-12+1.5)
cond3 = EDP(imax,3)>(StnLong-3) && EDP(imax,3)<(StnLong+3); % the longitude for this max value should be in the interval (-76.8-3,-76.8+3)
cond = (cond1 && cond2 && cond3); % combine all conditions together (must all be logical ones for validating and storing data)
%% main loop
added_table = []; % init empty
if cond % if all conditions are met ,=> add this table
% else = do nothing and go next file - no action needed !
end
EDPAll=[EDPAll;added_table]; % always add something either empty or (valid) non empty table
end
##### 댓글 수: 2표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Mathieu NOE 2022년 8월 22일
My pleasure !

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Abderrahim. B 2022년 7월 20일
Hi!
'After reading some data files where each file contains data stored in a table' >> EDP has only one table !!
Assuming your original EDP has many files ( tables), try the below code:
clear ;
EDP = struct2cell(EDP) ;
whos %
Name Size Bytes Class Attributes EDP 1x1 23711 cell
StnLat = -12 ;
StnLong = -76.8 ;
EDPALL = [] ;
for ied = 1:length(EDP)
[~,imax] = max(EDP{ied}.EDP6);
if EDP{ied}.EDP1(imax) < 190
% EDP(imax,:) = []; I did not understand why this line of code is here!!
continue
else
condiLatLong = (EDP{ied}.EDP2(imax)>(StnLat-1.5) && EDP{ied}.EDP2(imax) <(StnLat+1.5)) ...
&& (EDP{ied}.EDP3(imax)>(StnLong-3) && EDP{ied}.EDP3(imax) < (StnLong+3)) ;
if condiLatLong
fprintf('If statement is passed.\n')
EDPALL = [EDPAll; EDP{ied}];
else
fprintf('If statement is NOT passed.\n')
end
end
end
If statement is NOT passed.
Hope this helps

댓글을 달려면 로그인하십시오.

### 범주

Find more on Whos in Help Center and File Exchange

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by