I have a problem in performing a loop calculation
1 次查看(过去 30 天)
显示 更早的评论
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
%Load the data
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 个评论
采纳的回答
Star Strider
2023-9-22
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
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
.
3 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!