Why i get this following error when using dir in Matlab?
Matlab keep give me following error message :
??? Error using ==> dir Argument must contain a string.
Error in ==> Awal at 15 x = dir(subDirs)
Below is my codes :
%MY PROGRAM
clear all;
clc;
close all;
%-----Create Database-----
TrainDB = uigetdir('','Select Database Directory');
TrainFiles = dir(TrainDB);
dirIndex = [TrainFiles.isdir];
[s subDirNumber] = size(dirIndex);
for i = 3:subDirNumber
subDirs = {TrainFiles(i).name};
subDirs = strcat(TrainDB,'\',subDirs);
x = dir(subDirs) %<-------Error Here
end
Is something wrong with t开发者_如何学Gohe codes? Your help will be appreciated. I'm sorry for my bad English.
The problem is with this line:
subDirs = {TrainFiles(i).name};
When you strcat on the next line, you are strcat-ing two strings with a cell containing a string. The result in subDirs is a cell containing a string which dir() apparently doesn't like. You can either use
subDirs = TrainFiles(i).name;
or
x = dir(subDirs(1))
I would recommend the first option.
When I run your code I get the error message:
??? Error using ==> dir
Function is not defined for 'cell' inputs.
What MATLAB is telling you is that when you call dir(subDirs)
subDirs
is a cell rather than a string which is what dir
wants. Something like dir(subDirs{1,1})
will do what (I think) you want. I'll leave it to you to rewrite your code.
with subDirs = {TrainFiles(i).name};
you create a cell-array of stings. dir
is not defined for that type. Just omit the {}
around the name
BTW: Your code does not only list directories, but all files. Check find
on the isdir
attribute to get only directory's indices!
精彩评论