# MATLAB Basics video: UIGETFILE for selecting a file in a script, function or GUI11

Posted by Doug Hull,

This is one in a series of videos covering MATLAB basics. It is meant for the new MATLAB user. This video covers how to use UIGETFILE to make it easier to select a file from a script, function or GUI.
Other videos have been gathered here: https://blogs.mathworks.com/pick/category/video/ Other MATLAB Basics posts have been gathered here: https://blogs.mathworks.com/pick/category/matlab-basics/

Eli replied on : 1 of 11
Say I use uigetfile to get a filename that is in a path not currently part of the matlab path. When I execute the next line of code (load(fileName)), the file will not be loaded since it is not in the current path. How can I go about adding the path (automatically/ without additional user input)? Cheers, Eli
Bob replied on : 2 of 11
Eli, does this help?
  [f,p] = uigetfile;

Cheers Bob
Fredrik replied on : 3 of 11
Thanks for these excellent videos. However, I am having some problems. I am trying to use the uigetfile to load a matfile without a .mat extension into the matlab workspace. I have had no problem doing this using the simple line:
load 18-dots.nirs -mat;

however, when I try this with the uigetfile I keep on getting error messages. The scripts I have tried are:

fileName = uigetfile('*.nirs')


or

fileName = uigetfile('*.nirs')


Help is appreciated. Cheers, F
dhull replied on : 4 of 11
@Fredrik, What are the error messages? Doug
Fredrik replied on : 5 of 11
Doug, the error message I get for the first script is: "??? Error using ==> load Number of columns on line 1 of ASCII file C:\Documents and Settings\ft20\Desktop\Matlab Scripts for Nirs\Insert Stim Marks\22-dots_fixed.nirs must be the same as previous lines." and for the second I get: "??? Error using ==> load Unable to read file fileName: No such file or directory." Thanks, F
Fabian Clemens replied on : 6 of 11
I use uigetfile to locate a txt-file on the network (paths start with \\abd...). Once the file is selected, I'll process it using textread: pause on [newFile,newPath]=uigetfile('.txt', 'Select data file.'); location=[newPath,newFile]; %pause (2) Data=textread(location); I get the error message 'Error using exist: The first input must be a string' before the uigetfile even opens. I don't get an error when i store the whole program on a local drive rather than on a network drive. I compiled the program to run as a standalone and saved it on network drive - same folder with MCR. I've tried to pause between uigetfile and textread, but w/o success. Do i need to put something else in between? I even tried to remove 'newPath' from my file location to have it only look at the current folder: same error message. It runs fine, when started as standalone and saved on local HD. It runs fine when started with Matlab (saved locally or on network). It does not run fine, when started as standalone and saved on network. Any ideas? Thank you, Fabian
Sandeep replied on : 7 of 11
I need help. When I use ‘uigetfile’ it takes me to the folder where I have save my GUI. I want to select the file from different folder. I can do this for the first time but when I again use ‘uigetfile’ it takes me again to the same folder where I saved my GUI. I do not want this. I want, in second time it takes me to the same folder from where I select my first file. How I can do this. Please help me. I am new to GUI. Help is appreciated. cheers
Iqbal replied on : 8 of 11
Sorry i can't open this video. please send me the videos to my email.
Iqbal replied on : 9 of 11
my text file is like this : ; ENVI Output of ROIs (4.4) [Wed Aug 19 10:18:04 2015] ; Number of ROIs: 1 ; File Dimension: 212 x 204 ; ; ROI name: EVF: Layer: 10 titik sampel.shp ; ROI rgb value: {0, 0, 0} ; ROI npts: 10 ; ID X Y Lat Lon B1 B2 1 113 9 -6.904033 113.273571 0.0531 0.3688 2 68 13 -6.912415 113.179550 0.0505 0.4311 but i cant open using this script of your code using GUI : function Open1_Callback(hObject, eventdata, handles) fileName = uigetfile('*.txt') load fileName -txt; I try to open like this but the error like this : Error using load Unknown command option. Error in tes>Open1_Callback (line 102) load fileName -txt; Error in gui_mainfcn (line 96) feval(varargin{:}); Error in tes (line 42) gui_mainfcn(gui_State, varargin{:}); Error in @(hObject,eventdata)tes('Open1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating uicontrol Callback Please help. any suggestion, i am using matlab 2013a. thanks
Iqbal replied on : 10 of 11
Ok I try like this it works : [newFile,newPath]=uigetfile('.txt', 'Select data file.'); location=[newPath,newFile]; %pause (2) dataku=dlmread(location,'',8,0) but i want this data read in each lines. how do you do that??
Stuart McGarrity replied on : 11 of 11
Seems more of a file import issue now rather than a uigetfile issue. Could you say again what you want to read in? Could you give a sample of the file?

This site uses Akismet to reduce spam. Learn how your comment data is processed.