Data in subfolder cannot be load
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nurul Atifah
am 11 Mär. 2018
Kommentiert: Walter Roberson
am 12 Mär. 2018
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 11 Mär. 2018
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 Kommentare
Walter Roberson
am 12 Mär. 2018
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!