How to open multiple .dat files in Matlab and save them in the given name as .xlsx file per sheet

2 ビュー (過去 30 日間)
I attached 10 .dat files. Each of the files contain 40 years hourly data. Kindly help me with the matlab codes to open and save the in .xlsx formats using the file names individually in a file sheet by sheet. I will also use the codes for the other 30 files. Thanks.
Ojo O. S

採用された回答

Image Analyst
Image Analyst 2021 年 7 月 31 日
For some reason, I'm having trouble unzipping your data. Window10 won't allow it. In the meantime, try this:
% Specify the folder where the files live.
myFolder = 'D:\data files';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
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);
numFiles = length(theFiles)
Result = zeros(numFiles, 1);
for k = 1 : numFiles
baseFileName = theFiles(k).name;
[~, baseFileNameNoExt, ext] = fileparts(baseFileName);
inputFullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', inputFullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
data = readmatrix(inputFullFileName, 'x.);
outputFullFileName = fullfile(myFolder, baseFileNameNoExt, '.xlsx');
fprintf(1, 'Now writing %s\n', outputFullFileName);
xlswrite(outputFullFileName, data);
end
  10 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 1 日
Then simply set up your outputFullFileName, BEFORE the for loop. Then it will save into the same workbook all the time. Of course to precent overwriting you're going to have to change the cell reference, like
outputFullFileName = 'whatever.xlsx'
nextRow = 1;
for k = 1 : numFiles
baseFileName = theFiles(k).name;
[~, baseFileNameNoExt, ext] = fileparts(baseFileName);
inputFullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', inputFullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
data = readmatrix(inputFullFileName, 'x.);
cellReference = sprintf('A%d', nextRow);
fprintf(1, 'Now writing %s\n', outputFullFileName);
xlswrite(outputFullFileName, data);
% Increment the next row to be just below this data
nextRow = nextRow + size(data, 1);
end
Ojo Olusola
Ojo Olusola 2021 年 8 月 3 日
Thank you. This code work efficiently.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by