How to save multiple polts with the source file name using loop?
Mostrar comentarios más antiguos
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 comentario
Stephen23
el 11 de Feb. de 2023
Respuestas (1)
Rik
el 11 de Feb. de 2023
0 votos
You have all elements already in your code. Why not uncomment savefig and use files(i).name?
4 comentarios
Rajeev Kumar
el 11 de Feb. de 2023
Editada: Rajeev Kumar
el 11 de Feb. de 2023
Rik
el 11 de Feb. de 2023
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?
Rajeev Kumar
el 11 de Feb. de 2023
Rik
el 11 de Feb. de 2023
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.
Categorías
Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!