【问题标题】:Is there a way in Matlab where I can generate a list of folders that have a particular folder in them?在 Matlab 中有没有一种方法可以生成包含特定文件夹的文件夹列表?
【发布时间】:2021-08-14 02:55:29
【问题描述】:

我在 Matlab 中工作,我有一个患者目录。每个患者都有一个带有他们的 ID(“Patient ID”)的文件夹,在该文件夹中,有一个日期文件夹(“Date”)。在日期文件夹中,有更多的文件夹包含各种信息(例如CT_Image、PET_Image、MR_Image)。方案是这样的:

患者> Patient_ID> 日期> MR_Image

在 Matlab 中有没有一种方法可以生成一个列表,告诉我哪个“患者 ID”具有特定文件夹,哪些没有?因此,与其手动检查“患者”文件夹中的所有患者是否有“MR_Image”文件夹,我可以只检查列表的结果吗?

【问题讨论】:

  • 你的意思是你想做ls Patients/*/*/MR_Image之类的事情吗?
  • 是的,这几乎正是我想要的。我希望输出能够为所有患者提供布尔值或类似的值。前任。患者 1:真,患者 2:假

标签: list matlab indexing


【解决方案1】:
Here some code to scan you patients data.

Brahim 

function ListPatientsData(strPatients, strDataType)
if(nargin == 0)
    clc
    clear
    close all    
    strPatients = pwd;
    strDataType = 'MR_Image';
end
disp(['root folder is ' strPatients])

hPatientIDs = dir(strPatients);

nNbPatientIDs = size(hPatientIDs, 1);
disp(['nNbPatientIDs = ' num2str(nNbPatientIDs)])
disp(' ')

for nPatient = 1:nNbPatientIDs
    hPatientID = hPatientIDs(nPatient);
    if(hPatientID.isdir)
        strPatientID = hPatientID.name;
        
        if(strcmp(strPatientID,'.'))
        % Do nothing 
        elseif(strcmp(strPatientID,'..'))
        % Do nothing 
        else 
            disp(['Patient_ID = ' strPatientID])
            
            hDates = dir([strPatients '/' strPatientID]);
            nNbDates = size(hDates, 1);
            % Minus 2 coz '.' and '..'
            disp(['nNbDates = ' num2str(nNbDates-2)])
            
            for nDate = 1:nNbDates
                
                hDate = hDates(nDate);
                if(hDate.isdir)
                    strDate = hDate.name;
                    
                    if(strcmp(strDate,'.'))
                    % Do nothing 
                    elseif(strcmp(strDate,'..'))
                    % Do nothing 
                    else 
                        disp(['strDate = ' strDate])
                        
                        hDataTypes = dir([strPatients '/' ...
                                          strPatientID '/' ...
                                          strDate]);
                        nNbDataTypes = size(hDataTypes, 1);
                        % Minus 2 coz '.' and '..'
                        disp(['nNbDataTypes = ' num2str(nNbDataTypes-2)])
                        
                        for nDataType = 1:nNbDataTypes

                            hDataType = hDataTypes(nDataType);
                            if(hDataType.isdir)
                                strDataType = hDataType.name;
                                %
                                if(strcmp(strDataType,'.'))
                                % Do nothing 
                                elseif(strcmp(strDataType,'..'))
                                % Do nothing 
                                else 
                                    disp(['strDataType = ' strDataType])
                                                                        
                                    hDataFiles = dir([strPatients '/' ...
                                          strPatientID '/' ...
                                          strDate '/' ...
                                          strDataType '/*.*' ]);
                                    nNbDataFiles = size(hDataFiles, 1);
                                    % Minus 2 coz '.' and '..'
                                    disp(['nNbDataFiles = ' num2str(nNbDataFiles-2)])
                                    
                                end
                            end
                        end
                    end
                end
            end
            disp(' ')
        end
    end            
end

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多