필터 지우기
필터 지우기

How to load multiple .dat files into MATLAB?

조회 수: 18 (최근 30일)
Bianca Elena Ivanof
Bianca Elena Ivanof 2016년 3월 3일
댓글: Image Analyst 2022년 10월 25일
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일
편집: 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!

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by