Basic Matlab

September 22, 2012

DEMO : HOW TO BUILD FILES MANAGEMENT GUI WITH MATLAB TOOLBOX
function varargout = Explorer(varargin) % EXPLORER M-file for Explorer.fig % EXPLORER, by itself, creates a new EXPLORER or raises the existing % singleton*. % % H = EXPLORER returns the handle to a new EXPLORER or the handle to % the existing singleton*. % % EXPLORER('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in EXPLORER.M with the given input arguments. % % EXPLORER('Property','Value',...) creates a new EXPLORER or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Explorer_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Explorer_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help Explorer % Last Modified by GUIDE v2.5 22-Sep-2012 17:03:10 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Explorer_OpeningFcn, ... 'gui_OutputFcn', @Explorer_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT

[janshendry@gmail.com]

Page 1

Basic Matlab

September 22, 2012

% --- Executes just before Explorer is made visible. function Explorer_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to Explorer (see VARARGIN) % Choose default command line output for Explorer handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes Explorer wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line. function varargout = Explorer_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output;

% --- Executes on selection change in lstdrive. function lstdrive_Callback(hObject, eventdata, handles) % hObject handle to lstdrive (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = get(hObject,'String') returns lstdrive contents as cell array % contents{get(hObject,'Value')} returns selected item from lstdrive try global pathname; global filename; global listfiles; global filesadd; indexlist = get(handles.lstdrive, 'Value'); temp = listfiles{indexlist}; filesadd = strcat(pathname, temp); if isdir(filesadd) pathname = strcat(filesadd,'\'); listfiles = refresh(pathname, filename);

[janshendry@gmail.com]

Page 2

Basic Matlab

September 22, 2012

set(handles.lstdrive, 'string', listfiles); set(handles.txtBrowse, 'String', pathname); else axes(handles.axes1); set(handles.axes1, 'NextPlot', 'replace'); imshow(filesadd); set(handles.txtBrowse, 'String', filesadd); end catch end % --- Executes during object creation, after setting all properties. function lstdrive_CreateFcn(hObject, eventdata, handles) % hObject handle to lstdrive (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

% --- Executes during object creation, after setting all properties. function listbox2_CreateFcn(hObject, eventdata, handles) % hObject handle to listbox2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in btnPrev. function btnPrev_Callback(hObject, eventdata, handles) % hObject handle to btnPrev (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global pathname; global listfiles; status = 1; indexlist = get(handles.lstdrive, 'Value'); [filesadd, inc] = DynamicImage(pathname, status, indexlist, listfiles); set(handles.lstdrive, 'Value', inc); set(handles.txtBrowse, 'String', filesadd);

[janshendry@gmail.com]

Page 3

Basic Matlab

September 22, 2012

imshow(filesadd); catch end % --- Executes on button press in btnNext. function btnNext_Callback(hObject, eventdata, handles) % hObject handle to btnNext (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global pathname; global listfiles; status = 0; indexlist = get(handles.lstdrive, 'Value'); [filesadd, inc] = DynamicImage(pathname, status, indexlist, listfiles); set(handles.lstdrive, 'Value', inc); set(handles.txtBrowse, 'String', filesadd); imshow(filesadd); catch end function [filesadd, inc] = DynamicImage(pathname, status, indexlist, listfiles) switch status case 0 inc = indexlist + 1; if inc > length(listfiles) inc = 3; end case 1 inc = indexlist - 1; if inc < 3 inc = length(listfiles); end end temp = listfiles{inc}; filesadd = strcat(pathname, temp);

% --- Executes on button press in btnDel. function btnDel_Callback(hObject, eventdata, handles) % hObject handle to btnDel (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try warning off; global filesadd; global pathname; global filename;

[janshendry@gmail.com]

Page 4

Basic Matlab

September 22, 2012

pilih = questdlg('Are You Sure Want To Delete This File?', ... 'Information Delete', 'Okay dude', 'No dude', 'No dude'); switch pilih case 'Okay dude' delete(filesadd); end listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); catch end % --- Executes during object creation, after setting all properties. function txtBrowse_CreateFcn(hObject, eventdata, handles) % hObject handle to txtBrowse (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in btnBrowse. function btnBrowse_Callback(hObject, eventdata, handles) % hObject handle to btnBrowse (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global filename; global pathname; global listfiles; [filename, pathname] = uigetfile('*.*', 'Pilih File'); if ~isequal(filename,0) || ~isequal(pathname,0) set(handles.lstdrive, 'Enable', 'on'); set(handles.txtBrowse, 'String', pathname); listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); end catch end % This function to list all files name in current directory function listfiles = refresh(pathname, filename) try files = dir(strcat(pathname,'*.*')); m = 1;

[janshendry@gmail.com]

Page 5

Basic Matlab

September 22, 2012

n = 1; for k = 1:numel(files) if isdir(strcat(pathname,files(k).name)) listfiles1{m} = files(k).name; m = m + 1; else listfiles2{n} = files(k).name; n = n + 1; end end if length(listfiles2) < 2 listfiles = listfiles1; else listfiles = [listfiles1 listfiles2]; end catch end % --- Executes during object creation, after setting all properties. function btnBrowse_CreateFcn(hObject, eventdata, handles) % hObject handle to btnBrowse (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% --- Executes during object creation, after setting all properties. function btnRename_CreateFcn(hObject, eventdata, handles) % hObject handle to btnRename (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% --- Executes during object creation, after setting all properties. function btnNext_CreateFcn(hObject, eventdata, handles) % hObject handle to btnNext (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% --- Executes during object creation, after setting all properties. function btnPrev_CreateFcn(hObject, eventdata, handles) % hObject handle to btnPrev (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO)

[janshendry@gmail.com]

Page 6

Basic Matlab

September 22, 2012

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global pathname; global listfiles; global filename; uurl = get(handles.txtBrowse, 'String'); [bar, kol] = find(uurl == char(92)); if isdir(uurl) if kol(end) == length(uurl) uurl = uurl(1:length(uurl)-(kol(end)-kol(end-1))); else uurl = uurl(1:length(uurl)-(length(uurl)-kol(end))); end else uurl = uurl(1:kol(end-1)); end set(handles.txtBrowse, 'String', uurl); pathname = uurl; listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); catch end

% --- Executes on key press with focus on lstdrive and none of its controls. function lstdrive_KeyPressFcn(hObject, eventdata, handles) % hObject handle to lstdrive (see GCBO) % eventdata structure with the following fields (see UICONTROL) % Key: name of the key that was pressed, in lower case % Character: character interpretation of the key(s) that was pressed % Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed % handles structure with handles and user data (see GUIDATA) try global pathname; global listfiles; global filesadd; indexlist = get(handles.lstdrive, 'Value'); temp = listfiles{indexlist}; filesadd = strcat(pathname, temp); imshow(filesadd); set(handles.txtBrowse, 'String', filesadd); catch end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.

[janshendry@gmail.com]

Page 7

Basic Matlab

September 22, 2012

% --- Otherwise, executes on mouse press in 5 pixel border or over lstdrive. function lstdrive_ButtonDownFcn(hObject, eventdata, handles) % hObject handle to lstdrive (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global pathname; global listfiles; global filesadd; indexlist = get(handles.lstdrive, 'Value'); temp = listfiles{indexlist}; filesadd = strcat(pathname, temp); imshow(filesadd); set(handles.txtBrowse, 'String', filesadd); catch end

% -------------------------------------------------------------------function mnuClose_Callback(hObject, eventdata, handles) % hObject handle to mnuClose (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close all;

% -------------------------------------------------------------------function mnuAbout_Callback(hObject, eventdata, handles) % hObject handle to mnuAbout (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) msgbox('This is only a demo on how to use File Management Toolbox in Matlab');

% -------------------------------------------------------------------function mnuCreator_Callback(hObject, eventdata, handles) % hObject handle to mnuCreator (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) msgbox('Design and Program by Jans Hendry'); % -------------------------------------------------------------------function ctxCopyFile_Callback(hObject, eventdata, handles) % hObject handle to ctxCopyFile (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global filesadd; global TempCopy; global ctxstatus;

[janshendry@gmail.com]

Page 8

Basic Matlab

September 22, 2012

if ~isdir(filesadd) TempCopy = filesadd; ctxstatus = 0; end

% -------------------------------------------------------------------function ctxMoveTo_Callback(hObject, eventdata, handles) % hObject handle to ctxMoveTo (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global filesadd; global TempCopy; global ctxstatus; if ~isdir(filesadd) TempCopy = filesadd; ctxstatus = 1; end

% -------------------------------------------------------------------function Addition_Callback(hObject, eventdata, handles) % hObject handle to Addition (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% -------------------------------------------------------------------function mnuNewFold_Callback(hObject, eventdata, handles) % hObject handle to mnuNewFold (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global pathname; global filename; NewFolder = inputdlg('Write New Folder Name','New Folder',1); mkdir(pathname,NewFolder{1}); listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles);

function txtBrowse_Callback(hObject, eventdata, handles) % hObject handle to txtBrowse (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of txtBrowse as text % str2double(get(hObject,'String')) returns contents of txtBrowse as a double

[janshendry@gmail.com]

Page 9

Basic Matlab

September 22, 2012

try global filename; global pathname; global listfiles; [filename, pathname] = uigetfile('*.*', 'Pilih File'); if ~isequal(filename,0) || ~isequal(pathname,0) set(handles.lstdrive, 'Enable', 'on'); set(handles.txtBrowse, 'String', pathname); listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); end catch end

% -------------------------------------------------------------------function mnuPasteFile_Callback(hObject, eventdata, handles) % hObject handle to mnuPasteFile (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try global TempCopy; global pathname; global ctxstatus; global filename; pathname = PPaste(ctxstatus, pathname, TempCopy); listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); catch end function pathname = PPaste(ctxstatus, pathname, TempCopy) try switch ctxstatus case 0 TempPaste = pathname; copyfile(TempCopy, TempPaste,'f'); pathname = TempPaste; case 1 TempPaste = pathname; movefile(TempCopy, TempPaste,'f'); pathname = TempPaste; end catch end

% --------------------------------------------------------------------

[janshendry@gmail.com]

Page 10

Basic Matlab

September 22, 2012

function ctxRemove_Callback(hObject, eventdata, handles) % hObject handle to ctxRemove (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global filesadd; global pathname; global filename; ans1 = 'Yes, Dude'; ans2 = 'No, Dude'; title = 'Warning'; if isdir(filesadd) quest = 'Are You sure want to delete this folder and content(s) ?'; decide = questdlg(quest,title,ans1,ans2,ans2); switch decide case 'Yes, Dude' rmdir(filesadd,'s') end else msgbox('This is not a folder, choose a folder!!'); end listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles);

% -------------------------------------------------------------------function ctxDelete_Callback(hObject, eventdata, handles) % hObject handle to ctxDelete (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try warning off; global filesadd; global pathname; global filename; pilih = questdlg('Are You Sure Want To Delete This File?', ... 'Information Delete', 'Okay dude', 'No dude', 'No dude'); switch pilih case 'Okay dude' delete(filesadd); end listfiles = refresh(pathname, filename); set(handles.lstdrive, 'string', listfiles); catch end

% --------------------------------------------------------------------

[janshendry@gmail.com]

Page 11

Basic Matlab

September 22, 2012

function mnuFile_Callback(hObject, eventdata, handles) % hObject handle to mnuFile (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% -------------------------------------------------------------------function ctxHorisonFlip_Callback(hObject, eventdata, handles) % hObject handle to ctxHorisonFlip (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global filesadd; fliplr(filesadd); RESULT

[janshendry@gmail.com]

Page 12

Basic Matlab

September 22, 2012

I will make the tutorial about how to use MATLAB MENU EDITOR and MATLAB TOOLBAR EDITOR later. Also how to combine them with matlab files management toolbar. This work is simple yet valuable. Full of tricky in programming. @ thanks…

[janshendry@gmail.com]

Page 13