2017-10-29 61 views
1

我真的需要将多个CSV文件合并为单个文件的帮助。在第一时间,我设法运行此代码:将多个csv文件合并为一个使用matlab的错误

%% 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 

原来我不得不改变命令“MYDIR”,所以它可以在一个文件夹中选择多个文件,一个文件夹中不是所有的文件,该文件需要被处理。因此,我改变上面的代码:

%% 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 

并且有一个错误信息

STRUCT内容从非结构阵列的对象引用。

回答

1

有在你的第二个代码中的一些错误:

  • ,如果你只选择一个文件,uigetfilechar字符串返回其名称,如果选择多个文件,他们的名字在返回一个cellarray因此你必须管理它。您可以使用该功能class检查它
  • 在调用fopen(fullfile('finalCSVnew.csv'),'w')你不调用fullfile提供path因此好像没用
  • 你还管理情况,即你放弃的选择文件。在这种情况下,uigetfile返回的值是0

您可以在foloiwng方式更新代码

% Call uigetfile by specifying file name and path as output 
[f_name,f_path] = uigetfile('*.txt','Select the data file','MultiSelect','on'); % gets directory from any folder 
% Check for file selection abort 
if(~strcmp(class(f_name),'double')) 
     fido=fopen(fullfile(f_path,'finalCSVnew.txt'),'w'); % open output file to write 
    % check for the number of selected files 
    % if multiple file 
    if(strcmp(class(f_name),'cell')) 
     % Loop over the selected files 
     for i=1:length(f_name) 
     fidi=fopen(fullfile(f_path,f_name{i}));        % open input file 
     fwrite(fido,fread(fidi,'*char'));          % copy to output 
     fclose(fidi);               % close that input file 
     end 
    else 
     fidi=fopen(fullfile(f_path,f_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 
else 
    disp('File Selection Aborted') 
end 

替代解决方案

如果你只是要合并一些文件,您可以使用system函数来调用DOS命令。

% Call uigetfile by specifying file name and path as output 
[f_name,f_path] = uigetfile('*.txt','Select the data file','MultiSelect','on'); % gets directory from any folder 
% Check for file selection abort 
if(~strcmp(class(f_name),'double')) 
    fido=fullfile(f_path,'finalCSVnew.txt'); % open output file to write 
    % check for the number of selected files 
    % if multiple file 
    if(strcmp(class(f_name),'cell')) 
     % Loop over the selected files 
     for i=1:length(f_name) 
     system(['type ' fullfile(f_path,f_name{i}) ' >> ' fido]) 
     end 
    else 
     system(['copy ' fullfile(f_path,f_name) ' ' fido]) 
    end 
else 
    disp('File Selection Aborted') 
end 

希望这有助于

Qapla”

+0

谢谢你这么多@il_raffa。你是一个救星! –

+0

不客气,我很高兴我一直在帮助。 –

+0

嗨@il_raffa对不起,我想再次问你。我其实已经做了一个新的线程,但似乎我的问题是无法解决的。我想用同一个工作表将多个xls文件合并成一个新的xls文件。你知道该怎么做吗?我知道它与readtable()或xlsread()有关,但我不知道如何形成代码。谢谢你,很抱歉再次问你一个问题。 –

相关问题