Create multiple files via for loop
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ivan Mich
le 1 Mar 2021
Réponse apportée : Ivan Mich
le 2 Mar 2021
I have a question about a code. I use this script in order to read file and making calculations. Finally i export my data to an output (txt file). The point is That i would like to create multiple file by reading multiple files and creates multiple outputs.
clc
clear
filename='data1125.xlsx'
[d1,tex]= xlsread(filename);
A1=d1(:,1);
B1=d1(:,2);
C1=d1(:,3)
results=mean(C1)
A=[{filename(1:4)} num2str(results)]
writecell(A,[char(filename(1:4)), '.txt'],'Delimiter','tab')
I think I should use for loop such as:
filename= dir('*.xlsx')
for k = 1:numel(filename)
end
Could you please help me?
0 commentaires
Réponse acceptée
Plus de réponses (1)
Allen
le 1 Mar 2021
You can use uigetfile to easily select mutliple files and make the filename acquisition process a little more simple.
% Prompts user to select *.xlsx file(s), with the starting location as the active MATLAB folder.
% Returns the file path and filename(s).
msg = "Select all *.xlsx data file(s) for processing"
[path,files] = uigetfile(fullfile(pwd,'*.xlsx'),msg,"MultiSelect","on");
Added your normal code to a loop. This task can be simplified, but using this format to make interpretation easier. Also, it is recommended to use readmatrix instead of xlsread, as it is much quicker.
% If path is numeric, no files where selected with uigetfile function
if ~isnumeric(path)
file = cellstr(file); % Ensures that the varible is a cell array
% previous conversion needed to prevent errors if only a single file was selected
for i=1:length(filename)
filename = fullfile(path,file{i}); % Combines the path and filename
% Original code
[d1,tex] = xlsread(filename);
A1 = d1(:,1);
B1 = d1(:,2);
C1 = d1(:,3)
results = mean(C1)
A = [{filename(1:4)} num2str(results)]
writecell(A,[char(filename(1:4)), '.txt'],'Delimiter','tab')
end
end
0 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!