필터 지우기
필터 지우기

Use of textscan instead of load in a for loop

조회 수: 4 (최근 30일)
Isma_gp
Isma_gp 2018년 9월 6일
답변: Walter Roberson 2018년 9월 7일
Hi,
I'm at the moment loading data using the following code:
data = cell(1,n_sim);
for m_a = 1:n_sim
for m_b = 1:n_seed
data{1,m_a}{m_b} = load(fileList{1,m_a}{m_b}, 'A1:end');
end
end
This takes a long time since there are a lot of files and they are quite big. The resulting data cell is a 1xn_sim cell, and each cell has 1xn_seed cells inside. Each of those i.e. data{1,1} contains a 11000x25 double.
I would like to use textscan to get the same format on my results. The files that I'm reading have 25 columns and 11000 rows. Can I get some help with the scripting of texscan for this case?
Thanks
  댓글 수: 6
Walter Roberson
Walter Roberson 2018년 9월 7일
편집: Walter Roberson 2018년 9월 7일
It appears that in practice loading from a text file ignores any parameters that do not start with '-' . It would be better to remove the 'A1:end' from the load() in order to reduce confusion. In particular, all of the file will be read in, not just column 1.
Walter Roberson
Walter Roberson 2018년 9월 7일
Is the data space delimited or comma delimited ?

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 7일
ncol = 25;
fmt = repmat('%f', 1, ncol);
data = cell(1, n_sim);
for m_a = 1:n_sim
data_mb = cell(1, n_seed);
for m_b = 1:n_seed
fid = fopen('fileList{1,m_a}{m_b}', 'rt');
data_mb(m_b) = textscan(fid, fmt, 'CollectOutput', 1); %notice () not {} on output
fclose(fid);
end
data{1,m_a} = data_mb;
end

추가 답변 (1개)

Alexander Jensen
Alexander Jensen 2018년 9월 6일
I imagine that the reason why it takes a while is due to the lack of preallocation of memory, I would however do like this (I know it is not using textscan, but why would you? :D )
dims = [11000 25];
data = nan(n_sim, n_seed, dims(1), dims(2));
for m_a = 1:n_sim
for m_b = 1:n_seed
data(m_a,m_b,:,:) = load(fileList{1,m_a}{m_b}, 'A1:end');
end
end
  댓글 수: 1
Isma_gp
Isma_gp 2018년 9월 7일
Hi, thanks for this. It is still taking the same amount of time though.

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

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by