基于列数据中的唯一ID覆盖单行excel信息



每次执行某些函数时,我都会将结果存储在excel文件中。第一列的行包含每个主题的唯一ID。每当我执行函数时,新的结果都会自动附加到新行中。每当执行相同的信息时,我都想用警告符号覆盖包含唯一ID信息的行,例如"信息已经存在,你想覆盖它们吗"等。我在matlab中用"唯一"函数尝试过,但没有成功。我们非常感谢在这方面提供的任何帮助。

到目前为止,我的代码如下:

LastName = {'Sam';'John';'Bella';'Diana';'Kelly'};
Age = [48;53;58;80;29];
Smoker = logical([1;0;1;0;1]);
Height = [61;59;64;69;62];
Weight = [126;153;141;153;129];
BloodPressure = [104 95; 119 79; 115 85; 127 85; 112 81];
Table = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
writetable(Table,"BP_Analysis.xlsx","WriteMode","append","AutoFitWidth",false);
%Overwirte the rows "Lastname" if same results are executed again. 
data = readtable('BP_Analysis.xlsx','PreserveVariableName', true);
data.Properties.VariableNames{1} = 'Lastname';
[~,idx]=unique(strcat('Lastname','rows'));

如果再次执行具有相同姓氏的数据,我希望覆盖任何行中的所有信息。我创建了一个伪问题来反映我的原始数据。

使用上面片段中的xlsx文件,您可以执行以下操作:

如果新行中的LastName是,比如说"Sam",那么:

newLastName = 'Sam';
idx = contains(data.LastName, newLastName)

现在,idx将包含与"Sam"匹配的所有现有条目的行索引,因此您可以对它们进行索引以覆盖、添加警告等

if any(idx)
% throw warning, ...
end
for j = 1:numel(idx)
if idx(j) == 1
% replace the row:
data(j, :) = % ...
end
end

最新更新