Passing Data Between GUI and .m Script

32 views (last 30 days)
James on 16 May 2013
I'm new to MATLAB and don't yet have a full understanding of GUIDE. I wrote a .m script that gathers data from text files, where the only input is the folder path containing the files. I would like to make a GUI where the folder path can be entered into a text box and then passed to the .m script. So I am wondering how to pass variables between the GUI and my .m script. Thanks in advance!
  1 Comment
Iain on 16 May 2013
You might want to look at the function "uigetdir".
If you're using guide, each callback in the automatically generated code file will have a variable "handles" passed in. This contains the handle of everything in the gui. The properties of each item can be read by using the "get" function, and changed by using the "set" function.

Sign in to comment.

Answers (2)

Volkan Kandemir
Volkan Kandemir on 17 May 2013
It is better not to use GUIDE. Developing a GUI with a m file is not so hard. You can use GLOBAL variable to transfer and store the data.
for example
function myfirstgui
global MYGUI
'OuterPosition',[0.2 0.3 0.6 0.7],...
'Name','My Program');
'Style', 'edit',...
'String','Write a note for text box',...
'Units', 'normalized',...
'Position', [0 0.7 0.5 0.3],...
'FontSize', 12);
'Style', 'pushbutton',...
'String','Click Me to transfer string of text box to me',...
'Units', 'normalized',...
'Position', [0.5 0.7 0.5 0.3],...
'FontSize', 12,...
function button1clickfunction(~,~)
global MYGUI

Image Analyst
Image Analyst on 17 May 2013
"It is better not to use GUIDE." is not fact, and not my opinion. It's your opinion, and that's fine, but look at all those tedious parameters you have to code in manually if you do not use the GUI development tools available to you. Would someone new to MATLAB somehow know they have to set the 'Style' property or how to set the 'Callback' property? I don't have to worry about any of those, or if I do it's in a nice interactive GUI. Here are some tutorials to "guide" you through the process. That said, if you really want to "roll your own" you can look at this: Or better, check out MAGIC ( for a GUIDE-based framework that makes it easy.
Here's just the quick answer that directly answers your question:
% Browse for the file. Change starting folder as necessary.
folder = 'C:\Program Files\MATLAB\R2010b\toolbox\images\imdemos';
if ~exist(folder, 'dir')
folder = pwd;
fileSpecification = [folder '/*.*'];
% Ask user to specify the file.
[baseFileName, folder] = uigetfile(fileSpecification, 'Specify a file');
if baseFileName == 0
% Build up the full file name.
fullFileName = fullfile(folder, baseFileName);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!