How to save multiple polts with the source file name using loop?

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

Answers (1)

You have all elements already in your code. Why not uncomment savefig and use files(i).name?

4 Comments

'savefig and use files(i).name' is not worked for save the all graph.
Your comment sounds like your issue is solved, but you didn't mark my answer as accepted. Can you confirm that your issue is resolved?
'savefig and use files(i).name' is not worked for save the all graph.
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.

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 11 Feb 2023

Commented:

Rik
on 11 Feb 2023

Community Treasure Hunt

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

Start Hunting!