How to load multiple .dat files into MATLAB?

8 次查看(过去 30 天)
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 个评论
Bianca Elena Ivanof
编辑:Bianca Elena Ivanof 2016-3-3
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 2016-3-3
I think the script I gave in my answer below would be less tedious for the user.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-3-3
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 个评论
asem
asem 2022-10-24
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 2022-10-25
@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.

请先登录,再进行评论。

更多回答(2 个)

SnukeN3
SnukeN3 2019-4-26
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 个评论
Image Analyst
Image Analyst 2019-4-26
Replace
allDataArray = [allDataArray; dataArray]; % Must have same number of columns
with this:
allDataArray = [allDataArray, dataArray]; % Must have same number of rows

请先登录,再进行评论。


SnukeN3
SnukeN3 2019-4-27
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