How to save multiple polts with the source file name using loop?
1 次查看(过去 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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!