Filter löschen
Filter löschen

How to load multiple .dat files into MATLAB?

18 Ansichten (letzte 30 Tage)
Bianca Elena Ivanof
Bianca Elena Ivanof am 3 Mär. 2016
Kommentiert: Image Analyst am 25 Okt. 2022
Hey,
I have 53 .dat (000n_3.dat, 000n_50.dat; 000n+1_3.dat, 000n+1_50.dat; etc. - as you can see, their names are similar but not purely consecutive) files all stored in the same directory, which I want to import into MATLAB all at the same time. Loading each of them works (e.g. load(0002_03.dat)) but what I want is to have all 53 of them loaded into a giant .mat spreadsheet from where I can neatly copy and paste all the rows and columns into an Excel spreadsheet.
How can I do this?
Thanks in advance.
  3 Kommentare
Bianca Elena Ivanof
Bianca Elena Ivanof am 3 Mär. 2016
Bearbeitet: Bianca Elena Ivanof am 3 Mär. 2016
When you double click on it in workspace --> a spreadsheet, where you can edit datapoints and copy and paste all rows and columns at your convenience.
Image Analyst
Image Analyst am 3 Mär. 2016
I think the script I gave in my answer below would be less tedious for the user.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Mär. 2016
You can use the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F Use the second example. Then, inside the loop, read the data file with csvread(), readtable(), importdata(), or any of several other functions and append the data to your growing array. After the loop, create a filename and call xlswrite()
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Data'; % Wherever...
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.dat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in, plotting it, using it, or whatever.
dataArray = csvread(fullFileName); % Or whatever function you want.
% Append data
if k == 1
allDataArray = dataArray;
else
allDataArray = [allDataArray; dataArray]; % Must have same number of columns
end
end
xlswrite(outputFilename, allDataArray, 'All Data', 'A1');
  7 Kommentare
asem
asem am 24 Okt. 2022
hello @Image Analyst, thank you for your answer above, however, I have similar question but I want to have all the .dat file readout as table or array, when I used the code above, its only read the first file, and compined all the rest of the files in one arrays, any possiable way to do so? I hope its clear as I am new to matlab. thanks in advance.
Image Analyst
Image Analyst am 25 Okt. 2022
@asem csvread does give you the data as an array, so not sure why you think it doesn't. If you want the recommended function, use readmatrix. If you want a table, use readtable.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

SnukeN3
SnukeN3 am 26 Apr. 2019
I adapted this script to what I needed. It seems to be adding each file onto the bottom of the last. How do I get it to put new files into new columns?
  1 Kommentar
Image Analyst
Image Analyst am 26 Apr. 2019
Replace
allDataArray = [allDataArray; dataArray]; % Must have same number of columns
with this:
allDataArray = [allDataArray, dataArray]; % Must have same number of rows

Melden Sie sich an, um zu kommentieren.


SnukeN3
SnukeN3 am 27 Apr. 2019
Yep, that was it. Funny story, I tried the comma originally, but had mis-matched file lengths on the sample files I grabbed. Thanks for the help!

Community Treasure Hunt

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

Start Hunting!

Translated by