How to load a excel file in folder?

1 view (last 30 days)
Donghwan Kim
Donghwan Kim on 30 Aug 2016
Edited: Donghwan Kim on 1 Sep 2016
I wrote code which opens a csv file and progress some calculations. Now, I want to open csv files. It is hard to use 'for' loop ,because file names are irregular.
% d=dir('C:\Users\Donghwan\Desktop\mechanical loads');
c=d(3:9); %select csv files
for i=1:length(c)
x=csvread(char(c(i).name));
end

Accepted Answer

KSSV
KSSV on 30 Aug 2016
You can run a loop for all the (excel/csv) files in the folder. Go through the below lines of code:
xlfiles = dir('*.csv'); % You are in the folder of csv files/ change extension accordingly
Nfiles = length(xlfiles) ; % number of files
% loop for each file
for i = 1:Nfiles
fname = xlfiles(i).name ; % file name
data = xlsread(fname) ; % read the file
%%do what you want %%%
end
  1 Comment
Donghwan Kim
Donghwan Kim on 31 Aug 2016
Edited: Donghwan Kim on 1 Sep 2016
Thank you for your answer. It works very well.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!