How do I load multiple .dat files in matlab?

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!

 Risposta accettata

Stephen23
Stephen23 il 26 Apr 2018
Modificato: Stephen23 il 27 Apr 2018
"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:

4 Commenti

Thank you for your advice! That code generates this error on my end:
Undefined function or variable 'K'
How would I fix that?
@Sam Jones: change it to a lower case k, as my code shows.
Another question - is there any way to save the resulting files as matrices rather than cells? Or a way I can convert them/access the data. Each of my dat files should be a 600 x 3 matrix but I cannot access this.
Stephen23
Stephen23 il 27 Apr 2018
Modificato: Stephen23 il 27 Apr 2018
"is there any way to save the resulting files as matrices rather than cells?"
They are matrices. Matrices that happen to be in a cell array. A cell array is just a container, like a box that you put your favorite teddy bear in to... the box does not change what is put inside it: your teddy bear is still soft and cuddly inside a box. You put matrices into it, and so that is what is inside your cell array.
"Each of my dat files should be a 600 x 3 matrix but I cannot access this."
You can access the contents of any cell using cell array indexing:
for k = 1:numel(C)
C{k}
end
If all of the matrices are exactly the same size then you can concatenate them together into one 3D array:
A = cat(3,C{:});
and access each page of that array using indexing:
for k = size(A,3)
A(:,:,k)
end
In your original question you asked "Is there anyway I can load multiple .dat files at a time into individual matrices with their own names?": you really need to read the page that I linked to at the end of my answer. Using indexing, like in the examples I have shown you, is the simplest and most efficient way of storing/accessing this data.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

il 26 Apr 2018

Riaperto:

il 28 Apr 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by