Data in subfolder cannot be load

조회 수: 12 (최근 30일)
Nurul Atifah
Nurul Atifah 2018년 3월 11일
댓글: Walter Roberson 2018년 3월 12일
Hello, I'm using Matlab R2015b and having problem in reading all data from few sub folders. The folder/dir can be detected but it do not read/extract my data which is in form of '.DATA' and it is quite large data files. In each .DATA, the data is separated with space. I try to fix my coding but still the data cannot be read. I hope anybody can help me on where should i fix my code. I appreciate any help from you guys, thanks.
Here is the code:
f=dir(strcat(folderdir,'*.DATA'));
p={f.name};
temp = {};
for k=1: numel(p)
file= strcat(folderdir,p{k});
fid=fopen(file);
b=textscan(fid,'%f %f %f','delimiter',' ');
fclose(fid);
x=b{1};
y=b{2};
z=b{3};
data{k}=dataset(x,y,z);
temp{k}=data{k};
end
set{j-2} = temp;
setname{j-2} = foldername;
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 11일
We recommend using fullfile(folderdir, '*.DATA') and fullfile(folderdir, p{k}) instead of strcat .
You should replace
fid=fopen(file);
with
[fid, msg] = fopen(file, 'r');
if fid < 0
error('Cannot open file "%s" because "%s"', file, msg);
end
  댓글 수: 2
Nurul Atifah
Nurul Atifah 2018년 3월 12일
First of all, thanks for the reply. I had try to fix the code like what you have stated, it can run and no error but still the data cannot be read. Here is the code that i had fix as your advice and code of how i called the folder and subfolders to give you clear view of my code:
files = dir('TrainingData');
dirFlags = [files.isdir];
subfolders = files(dirFlags);
for j = 3: length(subfolders)
folder = fullfile(subfolders(j).name,'\');
foldername = subfolders(j).name;
folderdir = fullfile(files(1).name,'\',folder);
f=dir(fullfile(folderdir, '*.DATA'));
fil={f.name};
temp = {};
for k=1: numel(fil)
fullfile(folderdir,fil{k});
[fid, msg] = fopen(file, 'r');
if fid < 0
error('Cannot open file "%s" because "%s"', file, msg);
end
b=textscan(fid,'%f %f %f','delimiter',' ');
fclose(fid);
x=b{1};
y=b{2};
z=b{3};
data{k} =dataset(x,y,z);
temp{k} =data{k};
end
set{j-2} = temp;
setname{j-2} = foldername;
end
And there is red line under data{k},temp {k}, set{j-2} and setname{j-2} that saying the variable changes size on every loop iteration (within script). Consider preallocating for speed. May i know how could i solve this problem? Thank you.
Walter Roberson
Walter Roberson 2018년 3월 12일
projectdir = 'TrainingData';
files = dir(projectdir);
dirFlags = [files.isdir];
subfolders = files(dirFlags);
%do not assume that . and .. are the first two subfolders.
subfoldernames = {subfolders.name};
mask = ismember(subfoldernames, {'.', '..'});
subfoldernames(mask) = [];
subfoldernames = fullfile(projectdir, subfoldernames);
for j = 1 : length(subfoldernames);
foldername = subfoldernames{j};
f = dir( fullfile(foldername, '*.DATA') );
fil = fullfile(foldername, {f.name});
temp = {};
for k = 1: numel(fil)
filename = fil{k};
[fid, msg] = fopen(filename, 'r');
if fid < 0
error('Cannot open file "%s" because "%s"', file, msg);
end
b = textscan(fid,'%f %f %f','delimiter',' ');
fclose(fid);
x=b{1};
y=b{2};
z=b{3};
data{k} = dataset(x,y,z);
temp{k} = data{k};
end
set{j} = temp;
setname{j} = foldername;
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by