0

I am trying to run multiple .mat files in matlab. So far i have 3 .mat files each containing the same variables. I want to run one .mat file and then switch to the next file. They are named like this:

        file2019_1.mat %day 1
        file2019_2.mat %day 2
        file2019_3.mat %day 3

The code i have tried to run works for the first .mat file and it then doesnt switch to the second. Ideally i am trying to run all 3 files continuously, as in the future i could have 100s.

This is the code i tried so far:

        % set up folder for .mat files containing variables of interest 
        myFolder = ('filepath');
        filePattern = fullfile(myFolder, 'file2019_*.mat');
        fileList = dir(filePattern);

        % set up variable data (here it is daily mean velocity value) 
        % hourly, m/s (one mat file one day)
        number_mat = length(fileList);
        for i = 1:number_mat
        load(['file2019_' num2str(i) '.mat'])

        %%%% run model in here

        end 

Any help on how i could get this to run continuously through each mat file would be great. Thank you.

nico1234
  • 9
  • 8
  • Not to sure what could be an issue but here is a condensed way to read the `.mat` files: [GitHub Gist: A MATLAB Script for Loading MAT files from folders](https://gist.github.com/MichaelTr7/a24d6717d5b6c94121cef41409f0f5a3). The only thing I can see that may cause an issue is not concatenating the folder to the file being loaded. – MichaelTr7 Apr 08 '21 at 18:02
  • 1
    @MichaelTr7 thank you, i tried your code but it is still stopping when it should switch to the second .mat file. My model is tracking particles, and has a time stamp so not sure if this is the problem, or its restarting completely as now my error is to do with the size of arrays. – nico1234 Apr 08 '21 at 18:42
  • Could be an issue with array sizes. I think your `.mat` file loading is perfectly fine. This probably has to do more with the code within the for-loop. Feel free to add any code, errors or issues by [editing](https://stackoverflow.com/posts/67009088/edit) the question. – MichaelTr7 Apr 08 '21 at 18:45

1 Answers1

0

A very easy method , just select all the files (Ctrl + A) - drag and drop them into the command window (be sure to drag the first file to load them in same order).

Or You can use this

% Read files mat1.mat through mat20.mat
for k = 1 : 20  % or whatever your numbers of files
    % Create a mat filename, and load it into a structure called matData.
    matFileName = sprintf('mat%d.mat', k);
    if isfile(matFileName)
        matData = load(matFileName);
    else
        fprintf('File %s does not exist.\n', matFileName);
    end
    
end

Or

% Get a list of all txt files in the current folder, or subfolders of it.

fds = fileDatastore('*.txt', 'ReadFcn', @importdata)

fullFileNames = fds.Files

numFiles = length(fullFileNames)

% Loop over all files reading them in and plotting them.

for k = 1 : numFiles

    fprintf('Now reading file %s\n', fullFileNames{k});

    % Now have code to read in the data using whatever function you want.

    % Now put code to plot the data or process it however you want...

end