Passing Data Between GUI and .m Script

32 views (last 30 days)
James
James on 16 May 2013
Hi,
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!
James
  1 Comment
Iain
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
MYGUI.figure=figure();
set(MYGUI.figure,...
'Units','normalized',...
'OuterPosition',[0.2 0.3 0.6 0.7],...
'MenuBar','none',...
'Name','My Program');
MYGUI.textbox1=uicontrol(MYGUI.figure,...
'Style', 'edit',...
'String','Write a note for text box',...
'Units', 'normalized',...
'Position', [0 0.7 0.5 0.3],...
'FontSize', 12);
MYGUI.button1=uicontrol(MYGUI.figure,...
'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,...
'Callback',@button1clickfunction);
function button1clickfunction(~,~)
global MYGUI
text=get(MYGUI.textbox1,'String');
set(MYGUI.button1,'String',text);

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: http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples. Or better, check out MAGIC ( http://www.mathworks.com/matlabcentral/fileexchange/24224-magic-matlab-generic-imaging-component) 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;
end
fileSpecification = [folder '/*.*'];
% Ask user to specify the file.
[baseFileName, folder] = uigetfile(fileSpecification, 'Specify a file');
if baseFileName == 0
return;
end
% 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!