フィルターのクリア

Load multiple files to MATLAB without changing name manually

1 回表示 (過去 30 日間)
Yew Jen Chong
Yew Jen Chong 2022 年 6 月 13 日
コメント済み: Yew Jen Chong 2022 年 6 月 14 日
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.

採用された回答

Chunru
Chunru 2022 年 6 月 13 日
% 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 件のコメント
Yew Jen Chong
Yew Jen Chong 2022 年 6 月 14 日
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)};

サインインしてコメントする。

その他の回答 (1 件)

Karim
Karim 2022 年 6 月 13 日
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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by