Making loop for a function 'xlsread'

조회 수: 2 (최근 30일)
Abdul Hannan Qureshi
Abdul Hannan Qureshi 2024년 3월 5일
댓글: Abdul Hannan Qureshi 2024년 3월 5일
Hi,
I have many files
1.csv, 2.csv, 3.csv.................................39.csv
How can I make a simple loop to convert them all together.
I simply want to obtain
data1=xlsread('1.csv');
data2=xlsread('2.csv');
data3=xlsread('3.csv');
.
.
.
.
data39=xlsread('39.csv');
I have tried to make its loop but I am getting stuck.
q = dir('Data/*.csv');
filenames = {q.name};
nfiles = length(filenames);
for i=1:nfiles
thisfile=filenames{i};
end
Kindly if someone can assist.
regards
  댓글 수: 1
Stephen23
Stephen23 2024년 3월 5일
편집: Stephen23 2024년 3월 5일
Use a DataStore:
"I simply want to obtain"
Numbering lots of variable names like that is one way that beginners force themselves into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug. Best avoided.
The MATLAB approach is to use indexing. Indexing is simple and efficient, unlike what you are trying to do.

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 3월 5일
편집: Dyuman Joshi 2024년 3월 5일
Use readmatrix instead of the deprecated xlsread().
q = dir('Data/*.csv');
nfiles = numel(filenames);
for k=1:nfiles
%file name
str = fullfile(q(k).folder, q(k).name)
%read the data
arr=readmatrix(str);
%perform necessary/required operations on the data
end
You can also preallocate a numeric array or a cell array to store the data - Preallocation
Also, since you have files named sequentially, you can read them accordingly as in the 1st example here -https://in.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html
(Note that the method you have used is also mentioned in the documentation linked)
  댓글 수: 4
Stephen23
Stephen23 2024년 3월 5일
"How can I import each file as seperate."
They already are: every cell of MYDATA contains a separate array imported from your files. You can trivially process those arrays using e.g. a loop and indexing. For example, the 2nd file's data: MYDATA{2}. So easy.
In contrast your approach of having lots and lots and lots of numbered variables is going to force you into writing slow, complex, inefficient code (either by doing lots of copy-and-pasting or by dynamically accessing the variable names... either way is a waste of time). Best avoided.
Abdul Hannan Qureshi
Abdul Hannan Qureshi 2024년 3월 5일
Thanks @Stephen23 @Chuguang Pan and @Dyuman Joshi for guidance and assistance.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chuguang Pan
Chuguang Pan 2024년 3월 5일
편집: Chuguang Pan 2024년 3월 5일
xlsread is not recommended, you can use readtable/readmatrix/readcell depend on what type of data you want to read.
The following code may be helpful:
q = dir('Data/*.csv');
filenames = {q.name};
Datas=cell(numel(filenames),1);
for i=1:length(filenames)
Datas{i}=readtable(filenames(i));
end
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 3월 5일
Datas=cell(39,1);
Hard coding values is not optimal.
Use numel() or size().

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by