Error in merging multiple CSV files into single file

1 回表示 (過去 30 日間)
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 27 日
Hi! I need help regarding merging multiple CSV files into single file. At the first time, I managed to run this code :
%%Merge multiple CSV files into one CSV file
myDir = uigetdir % gets directory from any folder
d=dir(fullfile(myDir,'*.csv')); % retrieve the files
fido=fopen(fullfile('finalCSVnew.csv'),'w'); % open output file to write
for i=1:length(d)
fidi=fopen(fullfile(myDir,d(i).name)); % open input file
fwrite(fido,fread(fidi,'*char')); % copy to output
fclose(fidi); % close that input file
end
fido=fclose(fido); clear fid* d % close output file, remove temporaries
Turns out I have to change the command for "myDir" so it can select multiple file in one folder, not all file in one folder which need to be processed. So I change the code above to:
%%Merge multiple CSV files into one CSV file
myDir = uigetfile('*.csv','Select the data file','MultiSelect','on'); % gets directory from any folder
d=fullfile(myDir,'*.csv'); % retrieve the files
fido=fopen(fullfile('finalCSVnew.csv'),'w'); % open output file to write
for i=1:length(d)
fidi=fopen(fullfile(myDir,d(i).name)); % open input file
fwrite(fido,fread(fidi,'*char')); % copy to output
fclose(fidi); % close that input file
end
fido=fclose(fido); clear fid* d % close output file, remove temporaries
and there is an error message "Struct contents reference from a non-struct array object." Could you please help me to solve the problem? Thank you very much.

採用された回答

Image Analyst
Image Analyst 2017 年 10 月 29 日
See my code:
% Merge multiple CSV files into one CSV file
[filenames, folder] = uigetfile('*.csv','Select the data file','MultiSelect','on') % gets directory from any folder
% Create output file name in the same folder.
outputFileName = fullfile(folder, 'finalCSVnew.csv')
fidOutput = fopen(outputFileName, 'wt'); % open output file to write
for k = 1 : length(filenames)
% Get this file name.
thisFileName = fullfile(folder, filenames{k})
% Open input file:
fidInput = fopen(thisFileName);
% Read text from it
thisText = fread(fidInput, '*char');
% Copy to output file:
fwrite(fidOutput, thisText);
fclose(fidInput); % close the input file
end
fclose(fidOutput);
% Open Windows Explorer to this folder
winopen(folder);
  4 件のコメント
Image Analyst
Image Analyst 2017 年 11 月 11 日
Yeah, of course that won't work. Excel workbooks are not text files and so you can't use 'wt' in fopen() and expect to get sensible stuff. You're going to have to use xlsread() or readtable() to read the workbooks into variables, then write them out to a workbook, either to the same worksheet, or each workbook to new sheets in the output workbook. It could get a little complicated if the input workbooks have multiple sheets or charts in them.
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 11 月 11 日
編集済み: Kasih Ditaningtyas Sari Pratiwi 2017 年 11 月 12 日
Thank you for your reply, Image Analyst. I am now trying to import one excel file and it managed to read by using readtable(), with this code :
[files,folder] = uigetfile('*.xls','Select Files','MultiSelect','on');
output = fullfile(folder,'rainfall.xls');
addpath(dir);
for i = 1:numel(files)
ftab = readtable(files{i});
writetable(ftab,output,'Sheet',files{i});
end
But the code generates a new file with separate sheets. I want to make it in the same sheet. Do you know what should I modify in my code above? Thank you very much for your help.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 27 日
Change
d=fullfile(myDir,'*.csv'); % retrieve the files
to
d = dir(fullfile(myDir,'*.csv')); % retrieve the files
  1 件のコメント
Kasih Ditaningtyas Sari Pratiwi
Kasih Ditaningtyas Sari Pratiwi 2017 年 10 月 27 日
Thank you for your reply, Walter. "But still I got an error: Error using dir Function is not defined for 'cell' inputs." Turns out, the output of "d" there is cell format. I also attach the picture about the error message here

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

カテゴリ

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