Professional Documents
Culture Documents
Step 0: Write and debug the program that realizes the computational
model as a MATLAB script file.
Step 1: Plan the GUI. Design the layout and determine which variables
are set through the GUI and how they are presented to the user. Design
the output display.
Step 2: Create the GUI with GUIDE. This will result in the GUIDE-
generated *.fig and the GUI tool *.m files.
Step 4: Connect the function to the GUI. Add calls from the GUI tool
code to the primary model function and synchronize the GUI objects.
Waitbars Waitbar pop-ups show the user how much progress has been
made toward the completion of a computational task. The key MATLAB
command is waitbar. (Section 12.1).
The waitbar command creates a thermometer-type display in
a pop-up figure window that represents the progress of the calculation graphically.
<handle>=waitbar(0, '<message string>', 'Name', '<title>');
In each iteration of the loop, the waitbar command is invoked again to update the displayed
state
waitbar(progress, <handle>);
Here, progress should evaluate to a real number between 0 and 1, and < handle> is the
handle to the waitbar that has already been created.
Finally, the waitbar should be deleted by the command close(handle). The following
example illustrates the common pattern of waitbar use.
N=500;
hwb=waitbar(0, 'Calculating ...', 'Name', 'Time marching');
for k=1:N
The latter, functional, form of the command is necessary if the file name is stored in a string
variable.
fname='myData';
save(fname, 'y', 'yinit', 'yfinal');
The file thus produced is named myData.mat and is a MATLAB-specific binary file; it is
not a text file and in general cannot be read except by MATLAB. Note that the file contains
not only the data, but also the name and class of each of the variables. If you do not specify
a list of variables, all the variables in the current workspace are stored.
The information stored in a .mat file can be read back into MATLAB using the load
command.
load myData % loads all variables from myData.mat
load myData x y name % loads variables x, y, name from myData.mat
Or
theFile='myData';
load(theFile);
load(theFile, 'x', 'y', 'name')
The function returns the variable fname containing the name of the file the user chose. The
returned variable pname has the full path name to the chosen folder—and it’s often long.
For example:
the first argument is the fully qualified name of the file, including its full path name, and is
formed by concatenating the strings representing the path name and the file name. Notice
that in the string concatenation operation (using square brackets) the path name and file
name need to be in the reverse order from that returned by uiputfile.
To load information into the workspace, the command uigetfile provides comparable
functionality.
[FileName, PathName, FilterIndex] = ...
uigetfile(FilterSpec, DialogTitle, DefaultName)
load([pname, fname]);
Example:
% save a,b,c to *.mat file chosen by user
a=1.1;
b=2.2;
c=3.3;
[fname, pname]=uiputfile('*.mat', 'Save in .mat file');
1. Open the file with the fopen command; this associates an integer called the “file
identifier number” with the file. The file can be opened in several different access
modes (also called permissions).
2. Write data from currently defined MATLAB variables to the file using the fprintf
command. The data will be written as formatted text strings.
3. Close the file using the fclose command.
fid=open('DataFileName.txt', '<accessString>');
Ex:
a=randi([1, 10]);
b=7.2*sqrt(2);
c=3*pi;
name='Alfonzo';
fid1=fopen('Somedata.txt','a');
fprintf(fid1,'%d %f %5.3f %s \n', a, b, c, name);
fclose(fid1);
>> A=rand(3)
A=
0.8173 0.3998 0.4314
0.8687 0.2599 0.9106
0.0844 0.8001 0.1818
>> fprintf('%g \n', A)
0.817303
0.868695
0.0844358
0.399783
0.25987
0.800068
0.431414
0.910648
0.181847
To read data from a file, the file can be opened with a “read” access mode and read using
textscan.
fid1=fopen('MyRelevantData.dat', 'r');
theData=textscan(fid1, '%d %f %f %s');
fclose(fid1);
The command textscan uses the same format string conventions as fprintf and returns
a cell array with the data read by column. The first column of data is stored as a vector in
theData{1}, the second in theData{2}, and so forth.
avec=theData{1};
bvec=theData{2};
cvec=theData{3};
Names=theData{4};
disp('Display last row of data')
disp(['a=', num2str(avec(end))]);
disp(['b=', num2str(bvec(end))]);
disp(['c=', num2str(cvec(end))]);
disp(['Name=', Names{end}]);
x = inputdlg({'Name','Telephone','Account'},...
'Customer', [1 50; 1 12; 1 7]);
The first input to the command prompt is a cell array of strings that contains the prompts
for each value to be input. The name of the dialog box is displayed in the top border. If
numlines is 1, then each input is taken to be a scalar value (see the documentation for
inputting vector values). The defaultanswer is another cell array of strings, one for each
of the values being sought. The options structure includes fields for making the panel
resizable, and using the TEX interpreter so that here the prompt 'x_0' is rendered as x0 on
the dialog box.
The returned cell array of strings, caStr, must be processed to set the values of the desired
variables appropriately. If the user presses ‘Cancel’, inputdlg returns a null string (i.e., a
string with length zero).
if length(caStr)>0
x0=num2str(caStr{1});
v0=num2str(caStr{2});
disp(['x0= ', num2str(x0), ' v0=', num2str(v0)]);
end
Question dialogs The pop-up produced by the questdlg command
similarly allows the user to enter the answer to a simple yes/no question
(Section 12.5).
choice=questdlg('Do you want to sing the blues?','Important question');
wantsToSingTheBlues=false;
switch choice
case 'Yes'
disp('Then you gotta pay your dues.');
wantsToSingTheBlues=true;
case 'No'
disp('Okay, then.');
case 'Cancel'
disp('Cancelled.')
end
I am trying to send data (i.e. user input inside text field) between two different GUI's, bypassing
any intermediate GUI. I created a simple GUI to accomplish this after reading multiple forums
and reading the Matlab documentation, but I am still having problems. The GUI I created using
GUIDE, has a static text box in the "main" GUI. Then you press a push button to move to an
"intermediate" GUI that opens a "third" GUI. Finally, the last GUI has a input text box that
would change the value of the static text in the "main" GUI.
From everything I read, I believe the best way to accomplish this is by using setappdata and
getappdata.
First answer:
I believe I found a way to accomplish what I was trying to do... After calling the third GUI, I
added an If statement to check if data was added to the root. If something was added to the root
(data was recorded to be used in the main GUI), then immediately resume back to the main GUI.
It looks something like:
testGUI3('testGUI3', handles.figure1);
if isappdata(0,'ReturnTestText')
check = getfield(getappdata(0,'ReturnTestText'), 'testText')
uiresume(handles.figure1);
end
he third GUI has two options. Record data (from the input text box) once a button is pushed (i.e.
OK) or do nothing (i.e. CANCEL button).
%OK button
layers.testText = get(handles.test_edit1,'String');
setappdata(0,'ReturnTestText',layers);
uiresume(handles.figure1);
%CANCEL button
uiresume(handles.figure1);
Second Answer:
Others may be able to comment on the best way to do this using appdata. I have found, however,
that simply passing the handles structure from your main to secondary GUIs is a convenient way
of accomplishing this. This is how I do it:
In the callback in your main GUI that calls the secondary GUI:
function OpenSecondaryGui(hObject,eventdata,handles)
SG = SecondaryGui(handles);
set(SG,'WindowStyle','modal') %If you want to "freeze" main GUI until
secondary is closed.
uiwait(SG) %Wait for user to finish with secondary GUI.
handles = guidata(hObject); %If your secondary GUI updates some handles,
you'll need this line
Then, in the opening function of your secondary GUI, you'd do something like this:
function SecondaryGUI_OpeningFcn(hObject,eventdata,handles,varargin)
handles.mainHandles = varargin{1}; %Get passed-in handles structure and add
them to secondary GUI's handles structure
guidata(hObject, handles);
Finally, to pass data out of the GUI (such as through a user created "close" button, you save the portion
of you secondary handles that contains the primary structure back to your main figure (this assuming
that your opening function of the primary GUI saves the figure handle as "handles.main").
function CloseSecondary_Callback(hObject,eventdata,handles)
guidata(handles.mainHandles.main,handles.mainHandles)
You would just need to apply this another time to pass data through an intermediate to a third
GUI.
If this is going away from the way you prefer too much, no worries. I just figured I'd leave it as a
suggestion, as I've found it to be the cleanest way to share data between GUIs.