Load multiple files to MATLAB without changing name manually

1 view (last 30 days)
Hello, I have written a program for signal analyzing and I want to apply it to different files. Is there any ways to upload multiple files automatically without the needs of changing the file name manually as shown below?
cm = readtable('signal1.csv', 'VariableNamingRule','preserve');
Thank you if someone can assist me.

Accepted Answer

Chunru
Chunru on 13 Jun 2022
% list the files to be processed
fn = dir("signal*.csv");
for ifile=1:length(fn)
current_fn = fn(ifile).name;
% [file,path]=uigetfile('*.csv','MultiSelect','on');
cm = readtable(current_fn, 'VariableNamingRule','preserve');
cmVar = cm.Properties.VariableNames;
figure
plot(cm{:,1},cm{:,[2 3]})
grid
xlabel(cmVar{1})
legend(cmVar{[2 3]}, 'Location','best')
title('Original Signal')
env1 = envelope(cm{:,2}, 250, 'peak');
figure
plot(cm{:,1},cm{:,[2 3]})
hold on
plot(cm{:,1},env1,'LineWidth',2)
hold off
grid
xlabel(cmVar{1})
legend(cmVar{[2 3]},'Envelope','Location','best')
title('Original Signal with Envelope')
Threshold = 5;
Lv = env1 >= Threshold;
figure
plot(cm{Lv,1},cm{Lv,[2 3]})
grid
xlabel(cmVar{1})
legend(cmVar{[2 3]},'Location','best')
title('Edited Signal')
figure
Fs = 1/cm{2,1};
findpeaks(cm{Lv,2},Fs,'MinPeakDistance',0.016,'MinPeakProminence',15,'MinPeakHeight',15);
[pksRT,locsRT] = findpeaks(cm{Lv,2},Fs,'MinPeakDistance',0.016,'MinPeakProminence',15,'MinPeakHeight',15);
pksRT1 = num2cell(pksRT);
legend(cmVar{2},'Location','bestoutside')
text(locsRT,pksRT+5,pksRT1,'FontSize',8,'Rotation',90)
Period = diff(locsRT);
Period_mean = mean(Period);
Period_min= min(Period);
Period_max = max(Period);
impact= table (Period);
impact.Frequency = (1./Period)*60;
torque_rms = rms(cm{Lv,"Reaction Torque [Nm]"});
current_rms = rms(cm{Lv,"Current RMS [amp]"});
Step_res=stepinfo(cm.("Reaction Torque [Nm]"),cm.("Time [s]"))
Area_torque=cumtrapz(cm{Lv,"Time [s]"},cm{Lv,"Reaction Torque [Nm]"});
Overall_data = table(torque_rms,current_rms, Period_max, Period_min, Period_mean);
Overall_data.Area_torque = Area_torque(end);
% writetable (Overall_data, 'ML_Data.csv')
% writetable (impact, 'ML_Data.csv')
end
Step_res = struct with fields:
RiseTime: 2.9760e-04 TransientTime: 1.6248 SettlingTime: 5.0122 SettlingMin: -62.5159 SettlingMax: 89.4734 Overshoot: 8.6106e+04 Undershoot: 6.0233e+04 Peak: 89.4734 PeakTime: 1.5416
Step_res = struct with fields:
RiseTime: 1.3524e-04 TransientTime: 1.4950 SettlingTime: 5.0122 SettlingMin: -67.2468 SettlingMax: 85.3903 Overshoot: 9.6159e+04 Undershoot: 1.2223e+05 Peak: 85.3903 PeakTime: 1.3180
Step_res = struct with fields:
RiseTime: 6.5838e-04 TransientTime: 1.7612 SettlingTime: 5.0122 SettlingMin: -67.9828 SettlingMax: 81.1703 Overshoot: 1.6333e+05 Undershoot: 1.9514e+05 Peak: 81.1703 PeakTime: 1.3112
  1 Comment
Yew Jen Chong
Yew Jen Chong on 14 Jun 2022
Thank you @Chunru for your helping.
Another additional question hope you don't mind.
Do you know how to store the data for each loop?
I have tried but the variable only contain the data of the last loop.
size = [length(fn) 6];
varNames = ["Torque_rms","Current_rms","Period_max","Period_Min","Period_mean","Area_torque"];
varTypes = ["double","double","double","double","double","double"];
Overall_data = table('Size',size,'VariableTypes',varTypes,'VariableNames',varNames);
Overall_data(ifile,:) = {torque_rms,current_rms, Period_max, Period_min, Period_mean,Area_torque(end)};

Sign in to comment.

More Answers (1)

Karim
Karim on 13 Jun 2022
Hey, you can read the data via a loop, see below.
Best regards
numFiles = 3;
for nF = 1:numFiles
cm = readtable(['signal',num2str(nF),'.csv'], 'VariableNamingRule','preserve');
% put other process code here
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!