function varargout = FileAppend(varargin)
% UIMFAPPEND
% GUI application to append number of files to one file
%
% USAGE
%
% uimfappend
%
% Select the files that are to be appended into the listbox using add (+)
% Can remove unwanted files using '-'
% Sort it or move up and down depending on what order you want the
% files to be appended
% Select output file
% Click "Append" to append the files to the output file
%
% SEE ALSO fsplit, fappend, mfappend
% Written by Suresh Joel, Apr 15, 2003
if nargin == 0 % LAUNCH GUI
fig = openfig(mfilename,'reuse');
% Use system color scheme for figure:
set(fig,'Color',get(0,'defaultUicontrolBackgroundColor'));
% Generate a structure of handles to pass to callbacks, and store it.
handles = guihandles(fig);
set(handles.InFiles,'String',{},'Value',0)
guidata(fig, handles);
if nargout > 0
varargout{1} = fig;
end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
try
if (nargout)
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
else
feval(varargin{:}); % FEVAL switchyard
end
catch
disp(lasterr);
end
end
% --------------------------------------------------------------------
function varargout = InFiles_Callback(h, eventdata, handles, varargin)
%do nothing
% --------------------------------------------------------------------
function varargout = OutFile_Callback(h, eventdata, handles, varargin)
%do nothing
% --------------------------------------------------------------------
function varargout = To_Callback(h, eventdata, handles, varargin)
[outfile,outpath]=uiputfile('*.*','File to append files to');
if isequal(outfile,0) | isequal(outpath,0),
return; %If browse is cancelled
else
set(handles.OutFile,'String',strcat(outpath,outfile));
end;
% --------------------------------------------------------------------
function varargout = MoveUp_Callback(h, eventdata, handles, varargin)
handles.cur_file_ind=get(handles.InFiles,'Value');
if handles.cur_file_ind<=1,return;end;
filenames=get(handles.InFiles,'String');
c=filenames(handles.cur_file_ind-1);
filenames(handles.cur_file_ind-1)=filenames(handles.cur_file_ind);
filenames(handles.cur_file_ind)=c;
set(handles.InFiles,'String',filenames,'Value',handles.cur_file_ind-1);
guidata(gcbo,handles);
% --------------------------------------------------------------------
function varargout = MoveDown_Callback(h, eventdata, handles, varargin)
handles.cur_file_ind=get(handles.InFiles,'Value');
filenames=get(handles.InFiles,'String');
if handles.cur_file_ind>=length(filenames),return;end;
c=filenames(handles.cur_file_ind+1);
filenames(handles.cur_file_ind+1)=filenames(handles.cur_file_ind);
filenames(handles.cur_file_ind)=c;
set(handles.InFiles,'String',filenames,'Value',handles.cur_file_ind+1);
guidata(gcbo,handles);
% --------------------------------------------------------------------
function varargout = Add_Callback(h, eventdata, handles, varargin)
[filename,pathname]=uigetfiles('*.*','Select file to add');
filenames=cellstr(get(handles.InFiles,'String'));
%if(isempty(filenames)),
for i=1:length(filename),
filenames(length(filenames)+1)=cellstr(strcat(pathname,char(filename(i))));
end;
if length(filenames)==1,set(handles.InFiles,'Value',1);end;
%set(handles.
set(handles.InFiles,'String',filenames,'Value',1);
guidata(gcbo,handles);
% --------------------------------------------------------------------
function varargout = Append_Callback(h, eventdata, handles, varargin)
outfile=get(handles.OutFile,'String');
if(isempty(outfile)), msgbox('Please select output file'); return; end;
filenames=get(handles.InFiles,'String');
if(isempty(filenames)), msgbox('Please select input files'); return; end;
%check that output file is not one of input files, if so - dont append
for i=1:length(filenames),
if(strcmp(char(filenames(i)),outfile)),
msgbox('Output file cant be one of the input files');
return;
end;
end;
if(~strcmp(questdlg('Are you sure you want to append the listed files?'),'Yes')), return; end;
fid1=fopen(outfile,'w');
blk_sz=1024*1024; %1MB per copy
hw=waitbar(0,'Wait till all files are appended');
for i=1:length(filenames),
fid2=fopen(char(filenames(i)),'r');
c=fread(fid2,blk_sz,'uchar');
while(~isempty(c)),
fwrite(fid1,c,'uchar');
c=fread(fid2,blk_sz,'uchar');
end;
fclose(fid2);
waitbar(i/length(filenames),hw);
end;
fclose(fid1);
close(hw);
msgbox('Successfully appended files');
% --------------------------------------------------------------------
function varargout = Remove_Callback(h, eventdata, handles, varargin)
handles.cur_file_ind=get(handles.InFiles,'Value');
filenames=get(handles.InFiles,'String');
if(~isempty(filenames)),
filenames(handles.cur_file_ind)=[];
if get(handles.InFiles,'Value')>length(filenames),
set(handles.InFiles,'Value',handles.cur_file_ind-1);
elseif get(handles.InFiles,'Value')<=0
set(handles.InFiles,'Value',1);
end;
set(handles.InFiles,'String',filenames);
guidata(gcbo,handles);
end;
% --------------------------------------------------------------------
function varargout = Sort_Callback(h, eventdata, handles, varargin)
filenames=get(handles.InFiles,'String');
filenames=sort(filenames);
set(handles.InFiles,'String',filenames);
guidata(gcbo,handles);
% --------------------------------------------------------------------
function varargout = Clear_Callback(h, eventdata, handles, varargin)
if(~strcmp(questdlg('Are you sure you want to clear the list of files?'),'Yes')), return; end;
set(handles.InFiles,'String',{});