Info
此问题已关闭。 请重新打开它进行编辑或回答。
How do select values in one table which match those in another...
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Sindar
2020-10-16
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
回答(1 个)
Cris LaPierre
2020-10-17
编辑:Cris LaPierre
2020-10-17
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 个评论
Cris LaPierre
2020-10-19
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.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!