tags:

views:

47

answers:

3

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 the codes? Your help will be appreciated. I'm sorry for my bad English.

+1  A: 

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.

Justin Peel
Oh, yes. I did not notice if it was not a string but the cell. Thank you for your help Justin.
nata
@nata, You're welcome.
Justin Peel
+1  A: 

Hi

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.

High Performance Mark
Thanks for your advice, Mark
nata
+1  A: 

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!

king_nak

related questions