How to save multiple polts with the source file name using loop?
조회 수: 2 (최근 30일)
이전 댓글 표시
How to save multiple polts with the source file name using loop?
Code
folder = "C:\Users\91743\OneDrive\Documents\MATLAB\MATLAB\Files"; % specify the path to the folder
%filepattern=fullfile(folder,'.csv');
files = dir(fullfile(folder, '*.mat')); % get a list of all .mat files in the folder
nFiles = numel(files); % number of files in the folder
for i = 1:nFiles
signal=fullfile(folder, files(i).name); % load the signal from the .mat file
%x = signal; % extract the signal from the .mat file
% signal1=signal(:,1);
D = load(signal); % ugh ugh ugh, that ugly prefix on those poor variable names!
D = cell2struct(struct2cell(D),regexprep(fieldnames(D),'^\w\d+_?',''),1); % much better.
files(i).data = D;
D=[files.data];
current_signal=D(i).DE_time;
%signal1=struct2array(signal);
%field_names=getfield(signal);
%current_signal=field_names{1};
%disp(current_signal);
%current_signal1=load(current_signal'.mat');
%current_signal1=
Fs=51200; %sampling frequency
T=1/Fs; %sampling period
L=length(current_signal); %Lenght of the signal
%t=((0:L-1)*T);
req=((0:L-1)*Fs/L);
xdft1 = fft(current_signal); %Compute FFT
xdft1 = 1/L*xdft1; %Normalize
xdft1(2:end-1) = 2*xdft1(2:end-1);
X_mag = abs(xdft1);
freq=((0:L-1)*Fs/L);
%X = fft(signal); % compute the FFT of the signal
%magX = abs(X); % magnitude of the FFT
%phaseX = angle(X); % phase of the FFT
figure;
plot(freq,X_mag,'LineWidth',1);
xlim([0 1000]);
xlabel('Frequency (Hz)');
ylabel('Acceleration (g)');
%savefig([field_names num2str(i) '.fig']);
end
답변 (1개)
Rik
2023년 2월 11일
You have all elements already in your code. Why not uncomment savefig and use files(i).name?
댓글 수: 4
Rik
2023년 2월 11일
What code did you try and how did the result differ from what you want. I can't read your mind and I can't see your computer screen, so you will have to explain these things.
참고 항목
카테고리
Help Center 및 File Exchange에서 Vibration Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!