Matlab:匹配两个数据集Point_ID并生成含差值列的新表
基于Point_ID的数据集合并方案
数据集详情
数据集1(含差值信息)
| Point_ID | Record | Difference (m) |
|---|---|---|
| '2804AJGCA57' | 'Record003 - 220428_103738_Scanner_1 - 2804AJGCA57' | '0.035240' |
| '2804AJGCA28' | 'Record003 - 220428_103738_Scanner_1 - 2804AJGCA28' | '0.030961' |
| '2804AJGCA29' | 'Record003 - 220428_103738_Scanner_1 - 2804AJGCA29' | '0.030219' |
数据集2(含坐标信息)
| Point_ID | Easting | Northing | Elevation_OD |
|---|---|---|---|
| '2804AJGCA1' | '200305.3884' | '80809.76627' | '7.25913' |
| '2804AJGCA2' | '200304.9855' | '80809.20396' | '7.23274' |
| '2804AJGCA3' | '200304.3783' | '80808.51888' | '7.20207' |
需求说明
对比两个数据集的Point_ID列,若数据集2中的Point_ID在数据集1中存在,则将数据集1对应行的Difference (m)值添加到数据集2的对应行,或生成包含合并后信息的新表。
现有未完成代码
%% Import CSVs GCA_data = readtable('Input/Test/GCA&GCP_Results_Flight1.csv', 'Delimiter',';', 'Format','%s %s %s'); %Insert pathway to GCA_Results csv. XYZ_data= readtable('Input/Test/GCA&GCP_Flight1.csv','Delimiter',',','Format','%s %s %s %s'); %Insert pathway to the GCA XYZ file inputted into RiProcess. %% Pre - Settings ids = GCA_data.Object1; %Identifies all points that were used within the GCA calculations nids = numel(ids); % Identifies the number of unique point ids. gca_table = []; for ii = 1:nids; ID = ids{ii}; %Speicifies the point ID. idx = ismember() end
优化后的完整实现代码
MATLAB中可直接用join函数高效完成表合并,无需手动循环,实现代码如下:
%% 导入CSV文件 % 导入数据集1(差值数据) GCA_data = readtable('Input/Test/GCA&GCP_Results_Flight1.csv', ... 'Delimiter',';', 'Format','%s %s %s'); % 统一匹配列名称为Point_ID GCA_data.Properties.VariableNames{1} = 'Point_ID'; % 导入数据集2(坐标数据) XYZ_data = readtable('Input/Test/GCA&GCP_Flight1.csv', ... 'Delimiter',',','Format','%s %s %s %s'); % 统一匹配列名称为Point_ID XYZ_data.Properties.VariableNames{1} = 'Point_ID'; %% 基于Point_ID合并表 % 左连接:保留XYZ_data所有行,仅为匹配到的行添加差值列 merged_table = join(XYZ_data, GCA_data(:, {'Point_ID', 'Difference (m)'}), ... 'Keys', 'Point_ID', 'Type', 'left'); %% 输出结果 disp(merged_table); % 可选:将合并结果导出为CSV文件 writetable(merged_table, 'Output/Merged_GCA_XYZ.csv');
代码说明
- 先统一两个表的匹配列名称为
Point_ID,避免列名不一致导致匹配失败。 - 使用左连接模式,保证数据集2的所有行都被保留,未匹配到的行
Difference (m)列会显示为NaN。 - 可通过
writetable将合并结果导出为新文件,方便后续处理。
内容的提问来源于stack exchange,提问作者Adam Johns
相关产品推荐
相关产品推荐

