Info
This question is closed. Reopen it to edit or answer.
How do select values in one table which match those in another...
1 view (last 30 days)
Show older comments
I have two tables which look like the following. Where the MMSI number in column one of IMO_file matches Var4 in AISfile, I want to extract the MMSI, IMO and Callsign from IMO_file and add it to columns 5:7 of AIS file. I am having trouble doing this!
I have tried here below, but I cannot get the variables into the correct classes to allow this to work... and even then, is there a simpler way to do this than a forloop?
Thanks!
filtered_AIS=('Y:\AIS data\CPA_FILT');
filtered_AIS_files=dir(fullfile(filtered_AIS,'*.csv'));
%for each filtered AIS file, we wanna loop through relevant month of
%AIS_IMO
for a=1:length(filtered_AIS_files)
filename=filtered_AIS_files(a).name;
AISfile=readtable(fullfile(filtered_AIS,filename));
monthYR=strsplit(filename,{'_','.'});
monthYR=char(monthYR(3))
YRmonth=strcat(monthYR(4:7),monthYR(1:3));
IMO_AIS=strcat('Y:\AIS data\',YRmonth,'\input_data\not required');
IMO_files=dir(fullfile(IMO_AIS,'*.csv'));
for b=40:length(IMO_files)
pathparts=strsplit(IMO_files(b).name,'_');
if (pathparts(3)=='ITU5') %only ITU5 files have IMO data
opts=delimitedTextImportOptions;
IMO_file=readtable(fullfile(IMO_AIS,IMO_files(b).name),opts);
IMO_file=cell2table(IMO_file);
for c=1:length(AISfile)
ais_mmsi=AISfile(c,4);
for d=1:length(IMO_file)
itu5_mmsi=str2double(IMO_file(d,:));
if (ais_mmsi==itu5_mmsi)
AISfile(c,5:7)=IMO_file(d,1:3);
else
%do nothing
end
end
end
else
%do nothing
end
end
end
2 Comments
Sindar
on 16 Oct 2020
Also, I'm confused by these lines:
IMO_file=readtable(fullfile(IMO_AIS,IMO_files(b).name),opts);
IMO_file=cell2table(IMO_file);
readtable should already return a table, not a cell array
Answers (1)
Cris LaPierre
on 17 Oct 2020
Edited: Cris LaPierre
on 17 Oct 2020
If you are unfamiliar with joining tables, I suggest using the Join Tables task in the live editor. This allows you to interactively explore various settings until you get the result you want.
2 Comments
Cris LaPierre
on 19 Oct 2020
That's interesting. Your tables don't seem to be that large. Can you share your two tables in a mat file? You can attach them using the paper clip icon.
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!