Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Matlab中,目录递归遍历不超过2级_Matlab_Directory_Traversal - Fatal编程技术网

在Matlab中,目录递归遍历不超过2级

在Matlab中,目录递归遍历不超过2级,matlab,directory,traversal,Matlab,Directory,Traversal,目录递归遍历不超过2级。 为什么会这样 ============================================================= currentFolderDir = '.'; % pwd % path('C:\Users\EI\Documents\MATLAB\OO\Simple Object Creation in Object Oriented'); depthLevel = 0; folderCount = 0;

目录递归遍历不超过2级。 为什么会这样

============================================================= currentFolderDir = '.'; % pwd % path('C:\Users\EI\Documents\MATLAB\OO\Simple Object Creation in Object Oriented'); depthLevel = 0; folderCount = 0; fileCount = 0; fprintf('=====================================\n'); fprintf('Depth level: %d\n', depthLevel); [folderCount, fileCount] = fileDirectoryRecursiveTraversal (currentFolderDir, depthLevel, folderCount, fileCount); ============================================================= function [folderCount, fileCount] = fileDirectoryRecursiveTraversal (currentFile, depthLevel, folderCount, fileCount) for i = 1:depthLevel fprintf('\t\t'); end fprintf('%s\n', currentFile ); % Print the name of the entry %isdir(currentFile) % [ERR] can't go beyond level 2 %pause; if (isdir(currentFile)) % Check if it is a directory folderCount = folderCount + 1; fprintf('\nThere are %d folders.\n', folderCount); pause depthLevel = depthLevel + 1; fprintf('=====================================\n'); fprintf('Depth level: %d\n', depthLevel); % Get a list of all the entries in the directory entries = dir(currentFile); % entries(1).name = '.' % entries(2).name = '..' numberOfEntries = length(entries); % including current folder and pointer to folder 1 level up % Ensure that the list is not null % if( (numberOfEntries - 2) ~= 0 ) % 2: % entries(1).name = '.'; % entries(2).name = '..' if(numberOfEntries ~= 2) % Loop over all the entries for i = 3:numberOfEntries % Recursive call to traverse [folderCount, fileCount] = fileDirectoryRecursiveTraversal( entries(i).name, depthLevel, folderCount, fileCount); % i = 3:numberOfEntries end fprintf('\nDepth level: %d\n', depthLevel); fprintf('There are %d files.\n\n', fileCount); fileCount = 0; else % disp('cccccccccccccccccccc') fileCount = 0; % empty folder end else fileCount = fileCount + 1; folderCount = 0; end folderCount = folderCount - 1; depthLevel = depthLevel - 1; % exit level end ============================================================= currentFolderDir='。;%pwd %路径('C:\Users\EI\Documents\MATLAB\OO\Simple Object Creation in Object Oriented'); 深度层=0; folderCount=0; fileCount=0; fprintf('=======================================================\n'); fprintf('深度级别:%d\n',深度级别); [folderCount,fileCount]=fileDirectoryRecursiveTraversal(currentFolderDir,depthLevel,folderCount,fileCount); ============================================================= 函数[folderCount,fileCount]=fileDirectoryRecursiveTraversal(currentFile,depthLevel,folderCount,fileCount) 对于i=1:depthLevel fprintf('\t\t'); 结束 fprintf(“%s\n”,当前文件);%打印条目的名称 %isdir(当前文件)%[ERR]不能超出级别2 %停顿; if(isdir(currentFile))%检查它是否是一个目录 folderCount=folderCount+1; fprintf('\n共有%d个文件夹。\n',folderCount); 暂停 深度层=深度层+1; fprintf('=======================================================\n'); fprintf('深度级别:%d\n',深度级别); %获取目录中所有条目的列表 entries=dir(当前文件); %条目(1)。名称='。' %条目(2)。名称=“…” numberOfEntries=长度(条目);%包括当前文件夹和指向1级以上文件夹的指针 %确保列表不为空 %如果((numberOfEntries-2)~=0)%2:%entries(1).name='.';%条目(2)。名称=“…” if(numberOfEntries~=2) %循环遍历所有条目 对于i=3:numberOfEntries %遍历的递归调用 [folderCount,fileCount]=fileDirectoryRecursiveTraversal(条目(i).name,depthLevel,folderCount,fileCount);%i=3:numberOfEntries 结束 fprintf('\n第二级:%d\n',第三级); fprintf('有%d个文件。\n\n',文件计数); fileCount=0; 其他的 %disp(‘CCCC’) 文件计数=0;%空文件夹 结束 其他的 fileCount=fileCount+1; folderCount=0; 结束 folderCount=folderCount-1; 深度级别=深度级别-1;%出口水平 结束
我已经修改了一个用于递归处理特定目录中的文件的函数。它正确地遍历所有子目录并显示文件名,但不显示depthlevel、folderCount和fileCount。这应该很容易适应,但如果您需要帮助,请告诉我:

function processDirectory(path)

if ~strcmp(path(end), filesep)
    path(end+1)=filesep;
end
dirInfo= dir(path);
files=~[dirInfo.isdir];
fileNames={dirInfo(files).name};
disp(path);
if ~isempty(fileNames)
    for i=1:length(fileNames)
        % Do whathever (show file names?)
        disp(fileNames(i));
    end
end

% For each subdir, call itself again
isDir=[dirInfo.isdir];
dirNames={dirInfo(isDir).name};
dirNames(strcmp(dirNames, '.') | strcmp(dirNames, '..'))=[];

for i=1:length(dirNames)
    processDirectory([path dirNames{i} filesep]);    
end

我已经修改了一个用于递归处理特定目录中的文件的函数。它正确地遍历所有子目录并显示文件名,但不显示depthlevel、folderCount和fileCount。这应该很容易适应,但如果您需要帮助,请告诉我:

function processDirectory(path)

if ~strcmp(path(end), filesep)
    path(end+1)=filesep;
end
dirInfo= dir(path);
files=~[dirInfo.isdir];
fileNames={dirInfo(files).name};
disp(path);
if ~isempty(fileNames)
    for i=1:length(fileNames)
        % Do whathever (show file names?)
        disp(fileNames(i));
    end
end

% For each subdir, call itself again
isDir=[dirInfo.isdir];
dirNames={dirInfo(isDir).name};
dirNames(strcmp(dirNames, '.') | strcmp(dirNames, '..'))=[];

for i=1:length(dirNames)
    processDirectory([path dirNames{i} filesep]);    
end

尝试使用currentFolderDir=pwdAs注释,它已尝试,但也不起作用。entries=dir(当前文件);长度(条目)在第二级的深度显示“0”,这是不正确的行为。您没有将目录添加到文件中。您应该使用
fullfile(currentFile,entries(i).name)
-否则matlab找不到目录。尝试使用currentFolderDir=pwdAs注释,它已尝试,并且也不起作用。entries=dir(当前文件);长度(条目)在第二级的深度显示“0”,这是不正确的行为。您没有将目录添加到文件中。您应该使用
fullfile(currentFile,entries(i).name)
-否则matlab无法找到目录。