# I have a problem in performing a loop calculation

3 vues (au cours des 30 derniers jours)
ABDALLA AL KHALEDI le 22 Sep 2023
Commenté : Star Strider le 24 Sep 2023
I have this code which reads data from several excel files and extracts them into an array and performs a calculation on them then presents the results in a table, the problem now is that I need this calculation to be performed for for every 2000 data points (from 0.005 to 10 sec, note that the time vector in the excel file is from 0.005 to 81.92 with a step of 0.005). However, from my basic understanding, MATLAB uses double precision and there is a floating point so maybe using machine eps is a good approach instead of logical statements, the issue is that I have multiple files and I need the code to loop through each file and perform the below calculation plus and extra interval-by-interval calculation (the same calculation but for each interval instead of the total set of data) and store the results for the total and the intervals then presnt them in a table (so loop the files, then loop the vectors in the array to establish the intervals). I have had some AI sugeestions but I was not abble to determine how correct were they. Help is appreciated. I attached 3 data files for reference but I have like 400 files.
%Prompt the user to select a folder
folder_path = uigetdir('', 'Select the folder');
%Get a list of files in the folder
file_list = dir(fullfile(folder_path, '*.xlsx'));
%Initialize the output table
output_table = {};
%Loop through each file
for i = 1:numel(file_list)
% Get the file name
file_name = file_list(i).name;
%Total Torque RMS Calculation
tq = readmatrix(fullfile(folder_path, file_name));
%TRUE if values change (down to machine eps)
d = [true; diff(tq(:, 2)) ~= 0];
%Torque values at change points
TQ = tq(d, 2);
%Location of changes in logical vector
ix = find([d(:); true]);
%Difference in locations (number of occurrences)
N = diff(ix);
%Calculating the torque rms by groups weighted by time
%Removing roundoff in dt calculation to get the overall dt
dt = mean(diff(tq(:, 1)));
%Total Time
t_tot = dt * (size(tq, 1));
%Calculate the total tq_rms
tq_rms = sqrt(sum(TQ.^2 .* (N - 1) * dt) / t_tot);
newRow = {tq_rms};
%Add the newRow to the output_table
output_table = [output_table; newRow];
end
column_name = {'TQ_RMS'};
%Convert the output table to a table
output_table = cell2table(output_table, 'VariableNames', column_name);
%Display the results table
disp(output_table);
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Star Strider le 22 Sep 2023
I cannot understand exactly what you want to do or what the logic vector ‘d’ does. If you simply want to get the RMS value of the torque signal every 10 seconds, that is actually straightforward.
Try this —
% Files = dir(pwd)
Files = dir('*.xlsx');
Files.name
ans = 'H5T06Z010RPM.xlsx'
ans = 'H5T06Z020RPM.xlsx'
ans = 'H5T06Z510RPM.xlsx'
for k = 1:numel(Files)
T1 = readtable(Files(k).name, 'VariableNamingRule','preserve');
VN = T1.Properties.VariableNames;
t = T1{:,1};
t_bfr = buffer(t, 2000);
tq = T1{:,2};
tq_bfr = buffer(tq, 2000);
tq_rms = rms(tq_bfr);
figure
yyaxis left
plot(T1{:,1}, T1{:,2})
hold on
plot(t_bfr, (ones(2000,1)*tq_rms))
hold off
ylabel(VN{2})
yyaxis right
plot(T1{:,1}, T1{:,end})
ylabel(VN{3})
grid
xlabel(VN{1})
xlim([min(t) max(t)])
title(Files(k).name)
timeseg = [t_bfr(1,:); t_bfr(end,:)].';
DisplayTable = table(timeseg(:,1), timeseg(:,2), tq_rms(:), 'VariableNames',{'Start Time','End Time','Torque RMS'})
Results{k,:} = DisplayTable;
end
DisplayTable = 9×3 table
Start Time End Time Torque RMS ___________ ________ __________ 0.005 10 0.36623 10.005 20 0.41522 20.005 30 0.38779 30.005 40 0.38848 40.005 50 0.41168 50.005 60 0.38471 60.005 70 0.38019 70.005 80 0.41781 80.005 0 0.14823
DisplayTable = 9×3 table
Start Time End Time Torque RMS ___________ ________ __________ 0.005 10 0.51982 10.005 20 0.47605 20.005 30 0.5089 30.005 40 0.48032 40.005 50 0.50803 50.005 60 0.51684 60.005 70 0.5473 70.005 80 0.48055 80.005 0 0.25016
DisplayTable = 9×3 table
Start Time End Time Torque RMS ___________ ________ __________ 0.005 10 0.2419 10.005 20 0.3432 20.005 30 0.32467 30.005 40 0.30427 40.005 50 0.32322 50.005 60 0.32414 60.005 70 0.27453 70.005 80 0.265 80.005 0 0.11693
.
##### 3 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Star Strider le 24 Sep 2023
As always, my pleasure!
Thank you!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by