MATLAB: Error in opening file from another folder, how to fix this problem

directoryfile openfopenguiopenstructuicontroluicontrol callbackuigetfile

I have these codes to open and process a .txt file in MATLAB GUI:
[filename1,filepath1]=uigetfile({'*.txt*','Text Files'},...
'Select Data File');
cd(filepath1);
fp= fopen(filename1);
fgets(fp);
A = textscan(fp, '%f');
fclose(fp);
eee=A{:};
It works perfectly when opening a file in the same directory of the program, can open multiple files with same button. But when opening a second file in another folder directory, I get this error:
Undefined function 'myGUIprogram' for input arguments
of type 'struct'.
Error in
@(hObject,eventdata)myGUIprogram('pushbutton12_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Can anybody help me fix it? Thanks in advance!

Best Answer

After you cd() to a directory and handle the files there, cd() back.
Alternately, put the directory that contains myGUIprogram.m into the MATLAB path instead of counting on it to be in the current directory.
Better yet, don't do the cd()
fp = fopen(fullfile(filepath1, filename1))