error problem with extracting data

2 ビュー (過去 30 日間)
Hossein Shahbodaghkhan
Hossein Shahbodaghkhan 2019 年 1 月 12 日
回答済み: Hossein Shahbodaghkhan 2019 年 1 月 13 日
Hi everyone.
I'm very glad to join you on this incredible community...
I'm trying to write a program to training and testing database for my machine learning project.
in first step I wrote a m-file to export files.
here is the code:
function erpsplit(indir)
d = dir(indir);
filelist = {};
datadirection = {};
classname = {'dislike','like'};
observations = {};
for i = 1:length(d)
a = strcmp(d(i).name,classname{1});
b = strcmp(d(i).name,classname{2});
p = true;
q = true;
for m = 1:length(a)
p = p & a(1,m);
end
for m = 1:length(b)
q = q & b(1,m);
end
if (d(i).isdir == 0)
continue;
else if (p | q)
datadirection = cat(2,datadirection,{[indir '\' d(i).name]});
end
end
for k = [1,2]
d = dir(datadirection{k});
for i = 3:length(d)
if (d(i).isdir == 0)
filename = sprintf('%s\\%s',datadirection{k},d(i).name);
filelist{i-2,k} = filename;
end
end
end
for k = [1,2]
for i = 1:16
for j = 1:length(filelist)
data = load(filelist{j,k});
variables = fields(data);
observations{j,i,k} = data.(variables{1});
observations{j,i,k} = observations{j,i,k}(i,:);
end
end
end
s = 'Output Files';
mkdir(indir,s);
s = [indir '\' s];
for k = [1,2]
mkdir(s,classname{k});
s = [s '\' classname{k}];
for i = 1:16
str = ['ch#' num2str(i)];
mkdir(s,str);
s = [s '\' str];
for j = 1:length(filelist)
str = [classname{k} '_subject#' num2str(j) '_ch#' num2str(i) '.xls'];
var = observation{j,i,k};
s = [s '\\' str];
save(s,'var');
end
end
end
end
but an error occured during decoding the following line:
d = dir(datadirection{k});
it says:
Index exceeds array bounds.
Error in erpsplit (line 32)
d = dir(datadirection{k});
but I tracing the code and the "datadirection" cell looks just fine.
Thanks everyone for assisting!

採用された回答

Image Analyst
Image Analyst 2019 年 1 月 12 日
編集済み: Image Analyst 2019 年 1 月 12 日
At line 32, insert these lines:
whos k
whos datadirection
What do you see in the command window? Chances are datadirection has fewer cells than k.
  3 件のコメント
Image Analyst
Image Analyst 2019 年 1 月 13 日
Well, step through it line by line in the debugger and figure out why you never hit a line that assigns it to anything.
If you still can't figure it out, attach some files that your load() statement wants to read in and we'll do the same thing that you can do for you (that is, step through it line by line).
Hossein Shahbodaghkhan
Hossein Shahbodaghkhan 2019 年 1 月 13 日
Thank you very much for your assisting!
I finally found the error's cause and I made alot of changes for safty execution.
I attach my final code for everyone to use it if they need the same algorithm.

サインインしてコメントする。

その他の回答 (1 件)

Hossein Shahbodaghkhan
Hossein Shahbodaghkhan 2019 年 1 月 13 日
the final code:
function erpsplit(indir)
warning('off');
d = dir(indir);
filelist = {};
datadirection = {};
classname = {'dislike','like'};
observations = {};
for i = 1:length(d)
a = strcmp(d(i).name,classname{1});
b = strcmp(d(i).name,classname{2});
p = true;
q = true;
for m = 1:length(a)
p = p & a(1,m);
end
for m = 1:length(b)
q = q & b(1,m);
end
if (d(i).isdir == 0)
continue;
end
if (p | q)
datadirection = cat(2,datadirection,{[indir '\' d(i).name]});
end
end
for k = 1:2
d = dir(datadirection{k});
for i = 3:length(d)
if (d(i).isdir == 0)
filename = sprintf('%s\\%s',datadirection{k},d(i).name);
filelist{i-2,k} = filename;
end
end
end
for k = 1:2
for i = 1:16
for j = 1:length(filelist)
data = load(filelist{j,k});
variables = fields(data);
observations{j,i,k} = data.(variables{1});
observations{j,i,k} = observations{j,i,k}(i,:);
end
end
end
s = 'Output Files';
mkdir(indir,s);
s = [indir '\' s]; dir1 = s;
cd(s);
k = 1;
while (k <= 2)
a = strcmp(pwd,dir1);
p = true;
for m = 1:length(a)
p = p & a(1,m);
end
if (p)
mkdir(s,classname{k});
s = [s '\' classname{k}]; dir2 = s;
cd(s);
else
s = dir1;
cd(s);
continue;
end
i = 1;
while (i <= 16)
str = ['ch#' num2str(i)];
a = strcmp(pwd,dir2);
p = true;
for m = 1:length(a)
p = p & a(1,m);
end
if (p)
mkdir(s,str);
s = [s '\' str]; dir3 = s;
cd(s);
else
s = dir2;
cd(s);
continue;
end
for j = 1:length(filelist)
str = [classname{k} '_subject_n' num2str(j) '_channel_n' num2str(i) '.mat'];
var = observations{j,i,k};
f = [s '\\' str];
save(f,'var');
end
i = i+1;
end
k = k+1;
end
end

カテゴリ

Help Center および File ExchangePower and Energy Systems についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by