-1

I want to make a gui in which a push button is when pushed will search for a file/folder and when it does it will give that path to another function seperate which will do some processing after it gets a path. So far I did this

function pushbutton2_Callback(hObject, eventdata, handles)
....
....

folder_name=uigetdir('File Selector');

This browse the folder and now I want to give the path of that folder to this function

source_dir = 'here the path of that folder comes';  
source_files = dir(fullfile(source_dir, '*.txt/etc'));

In other words if I explain, that pushbutton I used starts executing this function but first it selects a file/directory to input to that function. I tried to use some examples but I am new in making gui so I am not successfull yet. Any help will appreciated. Thank you.

edit..

This is what I was trying to do.

function pushbutton2_Callback(hObject, eventdata, handles)
...
folder_name=uigetdir('File Selector');
global folder_path
folder_path=genpath(folder_name);

%% ftn where it will be used

function abc  %% this function is also called in another function. 

global folder_path

source_dir = folder_path;  %%% where all the files are placed
src_files = dir(fullfile(source_dir, '*.txt'));

then a loop to load all files

Muhammad
  • 67
  • 1
  • 1
  • 7
  • It's not quite clear, what exactly your problem is. Is it to pass the path in `folder_name` as an argument to a function? What does this function look like? Where have you defined it? Some more code would be helpful... – bushmills Jul 13 '16 at 09:50
  • @bushmills my intention is to get a folder path in folder_name and give that path to source_dir. I tried making the path of that folder as a global variable and passing that global variable into the source_dir but its not working. First I used to give the path in source_dir manually like ' c:\blah\blah' but now I making a gui in which a pushbutton will select the folder and pass the path of that folder in into that function which will start running. – Muhammad Jul 13 '16 at 09:57
  • So you want to do the following: 1. Get a folder path and store it in `folder_name`; 2. pass `folder_name` to a function, called in the pushbutton-callback function? 3. Pass the string of `folder_name` to `source_dir` Is this correct? – bushmills Jul 13 '16 at 10:00
  • @bushmills exactly – Muhammad Jul 13 '16 at 10:04

1 Answers1

0
function pushbutton2_Callback(hObject, eventdata, handles)

folder_name=uigetdir('File Selector');

listFiles(folder_name);

end

% put this function in the generated m-file
function listFiles(folder_name)

source_files = dir(fullfile(folder_name, '*.txt/etc'));

% do something with the files

end
bushmills
  • 673
  • 5
  • 17
  • its not working keep getting this error " Undefined function 'listFiles' for input arguments of type 'char'." is there any way we can make it global I am putting above what I tried to use. – Muhammad Jul 13 '16 at 10:53
  • you'll have to put all the functions in one m-file, so you won't get the undefined function error. Don't use global variables if you don't need to. For example, accidentally changes to global variables are often the cause for bugs, which are very hard to find. – bushmills Jul 13 '16 at 11:02
  • I tried again by making pathsname as global varaibles and its working now. You are right global varaibles are not a good choice but in my situation they are not used again and again just for storing filename and passing that name once to another function. – Muhammad Jul 13 '16 at 14:38