Thore - rather than using global variables, try changing your script into a function which accepts the path to and name of some file. For example,
function FixedLineString(filename, pathname)
if isempty(filename) || isempty(pathname)
[filename,pathname] = uigetfile('*.DAT','Select the .DAT file');
end
The above code allows for the filename and pathname to be optional so that if at least one is not provided, then the user is prompted to choose a file.
Now in your GUI, you would save the path to and the name of the file to your handles structure as
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.DAT');
set(handles.text3, 'String', filename);
handles.filename = filename;
handles.pathname = pathname;
guidata(hObject,handles);
Then, when you want to pass the path and name of the file to your (new) function, you would do
function Start_button_Callback(hObject, eventdata, handles)
pathname = [];
filename = [];
if isfield(handles,'pathname') && isfield(handles,'filename')
pathname = handles.pathname;
filename = handles.filename;
end
FixedLineString(pathname,filename);
In the above, we check to make sure that handles has fields for both pathname and filename before trying to access either. If both exist, then we pass both to your function. Else we pass empty input parameters which will cause your function to request the the user choose a file.
Try the above and see what happens!
As an aside, I noticed that in your callbacks you typically did
handles = guidata(hObject);
This is unnecessary as the third input to all callbacks is already the handles structure and so calling the above just duplicates what already has been done. Also, you only need to call
uidata(hObject, handles);
if you have updated the handles structure as we did in pushbutton1_Callback. Otherwise, there is no need to call it.
Best Answer