How to force Matlab to read files in a folder serially?

前端 未结 4 2068
独厮守ぢ
独厮守ぢ 2021-01-23 22:33

I have files in a folder that are numbered from writer_1 to writer_20. I wrote a code to read all the files and store them in cells. But the problem is

相关标签:
4条回答
  • 2021-01-23 22:35

    I don't know of any direct solutions to the problem you are having. I found a solution for a problem similar to yours, here

    List = dir('*.png');
    Name = {List.name};
    S = sprintf('%s,', Name{:});    % '10.png,100.png,1000.png,20.png, ...'
    D = sscanf(S, '%d.png,');       % [10; 100, 1000; 20; ...]
    [sortedD, sortIndex] = sort(D); % Sort numerically
    sortedName = Name(sortIndex);   % Apply sorting index to original names
    

    Differences are:

    1. You are dealing with directories instead of files
    2. Your directories have other text in their names in addition to the numbers
    0 讨论(0)
  • 2021-01-23 22:45

    Here is a slightly different way (edited to fix bug and implement suggestion of @Divakar to eliminate for loop)

    folders = dir(Path_training);
    folders(ismember( {folders.name}, {'.', '..'}) ) = [];
    
    %// Get folder numbers as cell array of strings
    folder_nums_cell = regexp({folders.name}, '\d*', 'match');
    
    %// Convert cell array to vector of numbers
    folder_nums = str2double(vertcat(folder_nums_cell{:}));
    
    %// Sort original folder array
    [~,inds] = sort(folder_nums);
    folders = folders(inds);
    
    0 讨论(0)
  • 2021-01-23 22:48

    Stealing a bit from DavidS and with the assumption that your folders all are of the form "writer_XX" with XX being digits.

    folders = dir([pwd '\temp']);
    folders(ismember( {folders.name}, {'.', '..'}) ) = [];
    
    % extract numbers from cell array
    foldersNumCell = regexp({folders.name}, '\d*', 'match');
    
    % convert from cell array of strings to double
    foldersNumber = str2double(foldersNumCell);
    
    % get sort order
    [garbage,sortI] = sort(foldersNumber);
    
    % rearrange the structure
    folders = folders(sortI);
    

    The advantage of this is that it avoids a for loop. In reality it only makes a difference though if you have tens of thousands for folders. (I created 50,000 folders labeled 'writer_1' to 'writer_50000'. The difference in execution time was about 1.2 seconds.

    0 讨论(0)
  • 2021-01-23 22:52

    Approach #1

    %// From your code
    folders = dir(Path_training);
    folders(ismember( {folders.name}, {'.', '..'}) ) = []
    
    %// Convert folders struct to a cell array with all of the data from dir
    folders_cellarr = struct2cell(folders)
    
    %// Get filenames
    fn = folders_cellarr(1,:)
    
    %// Get numeral part and sorted indices
    num=str2double(cellfun(@(x) strjoin(regexp(x,['\d'],'match'),''), fn(:),'uni',0))
    [~,ind] = sort(num)
    
    %// Re-arrange folders based on the sorted indices
    folders = folders(ind)
    

    Approach #2

    If you would like to avoid struct2cell, here's an alternative approach -

    %// Get filenames
    fn = cellstr(ls(Path_training))
    fn(ismember(fn,{'.', '..'}))=[]
    
    %// Get numeral part and sorted indices
    num=str2double(cellfun(@(x) strjoin(regexp(x,['\d'],'match'),''), fn(:),'uni',0))
    [~,ind] = sort(num)
    
    %// List directory and re-arrange the elements based on the sorted indices
    folders = dir(Path_training);
    folders(ismember( {folders.name}, {'.', '..'}) ) = []
    folders = folders(ind)
    

    Please note that strjoin is a recent addition to MATLAB Toolbox. So, if you are on an older version of MATLAB, here's the source code link from MATLAB File-exchange.

    0 讨论(0)
提交回复
热议问题