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!