How to process the data that have same row name

5 次查看(过去 30 天)
Hello. Currently I am doing a testing codes. But then, I found a loop problem which is too challenging for me since I am already facing it more than ten times (I am still a newbie). Below is my coding. My goals is to process the raw data and extract the features as stated in the code. Basically the problem that I am facing is that:
Error using tabular/vertcat Duplicate table row name: '2004.03.15.12.0'.
Error in Third_Test (line 55) Time_feature_matrix1 = [Time_feature_matrix1; df];
The file is too big so with that I share my link to GDrive: https://drive.google.com/drive/folders/1MltnJyAUh1BJpoPdNpqOUiHfhdbGF4wV?usp=drive_link
Here the Code:
% Initialize empty tables for each bearing
Time_feature_matrix1 = table();
Time_feature_matrix2 = table();
Time_feature_matrix3 = table();
Time_feature_matrix4 = table();
% Specify the test set and bearing numbers
test_set = 3;
bearing_numbers = [1, 2, 3, 4];
% Set the path to the directory containing the data files
path = 'file directory';
% Get list of files in the directory
files = dir(fullfile(path, '*'));
% Loop through the files in the directory
for j = 1:length(files)
filename = files(j).name;
if files(j).isdir % Skip directories
continue;
end
file_path = fullfile(path, filename); % Full path of the file
dataset = readtable(file_path, 'Delimiter', '\t', 'FileType', 'text'); % Read the dataset
% Loop through each bearing number
for j = 1:length(bearing_numbers)
bearing_no = bearing_numbers(j);
% Extract the bearing data
bearing_data = dataset{:, bearing_no};
% Calculate features
feature_matrix = [
max(bearing_data), % Max
min(bearing_data), % Min
mean(bearing_data), % Mean
std(bearing_data, 1), % Std
sqrt(mean(bearing_data.^2)), % RMS
compute_skewness(bearing_data), % Skewness
compute_kurtosis(bearing_data), % Kurtosis
max(bearing_data) / sqrt(mean(bearing_data.^2)), % CrestFactor
sqrt(mean(bearing_data.^2)) / mean(bearing_data) % FormFactor
];
df = array2table(feature_matrix.'); % Transpose for correct orientation
df.Properties.VariableNames = {'Max', 'Min', 'Mean', 'Std', 'RMS', 'Skewness', 'Kurtosis', 'CrestFactor', 'FormFactor'};
df.Properties.RowNames = {[filename(1:end-4)]}; % Append bearing number
df.Properties.RowNames = {rowName};
% Append the table to the corresponding bearing's feature matrix
switch bearing_no
case 1
if ~ismember(rowName, Time_feature_matrix1.Properties.RowNames)
Time_feature_matrix1 = [Time_feature_matrix1; df];
end
case 2
if ~ismember(rowName, Time_feature_matrix2.Properties.RowNames)
Time_feature_matrix2 = [Time_feature_matrix2; df];
end
case 3
if ~ismember(rowName, Time_feature_matrix3.Properties.RowNames)
Time_feature_matrix3 = [Time_feature_matrix3; df];
end
case 4
if ~ismember(rowName, Time_feature_matrix4.Properties.RowNames)
Time_feature_matrix4 = [Time_feature_matrix4; df];
end
% case 1
% Time_feature_matrix1 = [Time_feature_matrix1; df];
% case 2
% Time_feature_matrix2 = [Time_feature_matrix2; df];
% case 3
% Time_feature_matrix3 = [Time_feature_matrix3; df];
% case 4
% Time_feature_matrix4 = [Time_feature_matrix4; df];
end
end
end
% Define function to compute skewness
function skewness = compute_skewness(x)
n = length(x);
third_moment = sum((x - mean(x)).^3) / n;
s_3 = std(x, 1)^3;
skewness = third_moment / s_3;
end
% Define function to compute kurtosis
function kurtosis = compute_kurtosis(x)
n = length(x);
fourth_moment = sum((x - mean(x)).^4) / n;
s_4 = std(x, 1)^4;
kurtosis = fourth_moment / s_4 - 3;
end
  3 个评论
Rusyaidi Yusri
Rusyaidi Yusri 2024-5-31
Hi, sorry about that. Already open for everyone with the link. It should available right now.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2024-5-31
Rather than trying to create a table with duplicate RowNames (which as you see from the error message is not allowed) I'd store that information in a variable in the table itself (I'll call this bearingID based on the comment "% Append bearing number") Then you could probably just use one of the grouping functions from Grouping and Binning Data section on this documentation page (like groupsummary) with the bearingID variable as the grouping variable.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by