The loop in my code is taking longer time. Any way to make it faster? or any other faster way?

I have to read almost 22 files (.asc format). I made a loop to put all those in a cell array and make the histogram. But unfortunately, this is taking almost 5 minutes!!! This is because the files are larger in size. If I am not making a loop and call them in separate lines (load),without making a cell, concatenate each line into a single matrix, it take only less than 3 minutes. But calling 22 files in separate lines make the code much longer and difficult if I have even more files! Please suggest me a faster method. My code is given below:
for k=1:22
A{k} = load(['E:/MatlabData/Data/file',num2str(k),'.asc']);
end
B=cat(1,A{:});
This code is easy but too slow (~5 minutes if I have 22 files). Instead of this code, if I call the data like following:
A=load('E:/MatlabData/Data/file1.asc');
B=load('E:/MatlabData/Data/file2.asc');
...
...
...
V=load('E:/MatlabData/Data/file22.asc');
B=cat(1,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V);
This make the code faster. Any other way than this to call the files and concatenate in a faster way?

6 Comments

What happens when you pre-allocate the cell
A=cell(1,22);
for k=1:22
A{k} = ...
end
B=cat(1,A{:});
@Geoff Hayes no... different files have different number of lines.. each file has two columns and several number of rows. number of rows vary from 1000 to 10000 or even more.
Of the roughly five minutes that it takes to perform the task, where is the majority of the time spent - in the for loop for opening the 22 files, or in the concatenation that follows?
I just created 22 files with 25000x2 matrix in each, and it took seconds to read the files and create the 550000x2 result (using your code from above). Is the E drive (the path to your files) a local drive on your workstation?
@Geoff Hayes: Yes, drive E is in my computer. The file size vary from 185 kB to 145 MB! Thank you.

Answers (0)

This question is closed.

Tags

Asked:

on 6 Jun 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!