Search code examples
matlabdirectoryloadscanning

Scan files in a Directory - MATLAB


I am trying to load files from my directory with matlab. The code is fairly simple :

for j =1:8
    people_names=dir('~/Desktop/Directory/Data/*.mat');
    people_name=people_names(j).name
    resp=load('~/Desktop/Directory/Data/people_name');

However, the load command fail because it reads "people_name" as a string and not its value.


Solution

  • D'oh. Your first statement in your for loop should be outside of it. You want to find all files first, then loop over each file. You're doing that inside your loop statement, and that will probably not give you what you want.

    You also are using load wrong. You'd want to use the actual string of people_name itself. You also will want to loop over all possible file names, not just the first 8:

    people_names=dir('~/Desktop/Directory/Data/*.mat'); %// Change
    
    for jj = 1:numel(people_names) %// Change
    
        people_name=people_names(jj).name;
        resp=load(people_name); %// Change
    
        %// Rest of your code here....
        %//...
    end