MATLAB: How to load multiple .dat files in matlab

dat files

I have a number of .dat files i want to load into matlab. I am able to load one at a time using the following code:
[filename directory_name] = uigetfile('*.dat', 'Select a file');
fullname = fullfile(directory_name, filename);
trial1 = load(fullname);
However I want to load multiple files. If I change the code to this, I can select multiple files:
[filename directory_name] = uigetfile('*.dat', 'Select One or More Files', 'Multiselect', 'on');
fullname = fullfile(directory_name, filename);
trial1 = load(fullname);
But I get the following error:
Error using load
Must be a string scalar or character vector.
Is there anyway I can load multiple .dat files at a time into individual matrices with their own names? Any help is appreciated!

Best Answer

"How do I load multiple .dat files in matlab?"
Just follow the advice given in the MATLAB documentation. It shows two methods, and has examples too:
For example, you could do something like this:
[N,D] = uigetfile('*.dat', 'Select One or More Files', 'Multiselect', 'on');
out = cell(1,numel(N));
for k = 1:numel(N)
C{k} = load(fullfile(D,N{k}));
end
"Is there anyway I can load multiple .dat files at a time into individual matrices with their own names?"
Well, yes, there are ways of doing that... but that would slow, complex, buggy, and hard to debug code. Much simpler is to use one variable and indexing, like the code above. That is what the examples in the documentation show, and it is what you should do too. Read this to know more abut why: