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

2 views (last 30 days)
Salma fathi on 20 Jul 2022
Commented: Mathieu NOE on 22 Aug 2022
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 on 20 Jul 2022
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
Salma fathi on 23 Jul 2022
yes this is exactly what I needed. thank you very very much.
Mathieu NOE on 22 Aug 2022
My pleasure !

Abderrahim. B on 20 Jul 2022
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

### Categories

Find more on Logical 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!