Create multiple files via for loop
13 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(1 个)
Allen
2021-3-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Search Path 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!