I think this will be the last part of GUI in MATLAB series unless I remember something I missed.
I hope the stuff I covered so far have helped whoever out there in some way.
If not, oh well.
Last time I showed you how to save data to the on-screen objects, like a monitor screen, a window you just opened, axes, or plots. (Ok, I just showed how to save things on a monitor screen, but the idea is the same for others as well)
Today, I'll demonstrate how to save data in a storage and retrieve it later.
As usual, the source code is at the bottom of this posting.
Let's start with opening a figure and placing things on it.
Then assign what the buttons should do when pressed.
As you see here, the last three buttons are not doing anything. I just made them because I didn't like to have too much empty space on a corner.
Now, let's write a local function for the first button. This button is supposed to open a data file and import the data, but since I don't have any sample data, I made series or random numbers. Then save it in storage spaces called, x, y1, y2, y3, and y4.
It's time to write instructions for the rest of buttons. When the plot buttons are pressed, it gets data from the storage and plot them on the axes. Here you see I did not specify which axes to put the plots on because there's only one axes. Someday, if you work with more than one axes, be sure to specify it along with plot.
The end result looks like this. Start with "Get Data" button and play with the rest of plotting buttons.
Now you see there isn't much too GUI in MATLAB. It's just a bunch of somewhat unfamiliar keywords, and the concept is fairly straight forward.
Well, of course there is much more to the GUI in MATLAB but I think this is a good place to start with it.
One more thing. Compared to the text-only MATLAB codes, you see that the GUI takes more work time and effort to write a code. One time I heard someone saying, "To make a program easy to use, the programmer should put more time and effort to write it." He was talking about GUI.
But the upside of this is that once you write it, you won't have to tell others how to use it much. How convenient!!
I'll add more to the GUI in MATLAB if any of you reading this have a question aspired by a burning curiosity. But until then, this is it.
Have fun with it.
Source code:
function STORAGE
F1 = figure('Position',[100 200 700 500],'toolbar','none','menubar','none');
A1 = axes('Parent',F1,'unit','pixel','position',[50 50 500 440]);
B1 = uicontrol('Parent',F1,'style','pushbutton','position',[560 460 130 30]);
B2 = uicontrol('Parent',F1,'style','pushbutton','position',[560 420 130 30]);
B3 = uicontrol('Parent',F1,'style','pushbutton','position',[560 380 130 30]);
B4 = uicontrol('Parent',F1,'style','pushbutton','position',[560 340 130 30]);
B5 = uicontrol('Parent',F1,'style','pushbutton','position',[560 300 130 30]);
B6 = uicontrol('Parent',F1,'style','pushbutton','position',[560 260 130 30]);
B7 = uicontrol('Parent',F1,'style','pushbutton','position',[560 220 130 30]);
B8 = uicontrol('Parent',F1,'style','pushbutton','position',[560 180 130 30]);
set(B1,'string','Get Data','callback',{@LOCALGetData F1})
set(B2,'string','Plot 1st Data','callback',{@LOCALPlot F1 1})
set(B3,'string','Plot 2nd Data','callback',{@LOCALPlot F1 2})
set(B4,'string','Plot 3rd Data','callback',{@LOCALPlot F1 3})
set(B5,'string','Plot 4th Data','callback',{@LOCALPlot F1 4})
set(B6,'string','This doesn''t do anything')
set(B7,'string','This doesn''t do anything')
set(B8,'string','This doesn''t do anything')
function LOCALGetData(varargin)
% Since I don't have data file, I'll just generate something random.
F_Handle = varargin{3};
x = 1:10;
y1=rand(1,10);
y2=rand(1,10);
y3=rand(1,10);
y4=rand(1,10);
setappdata(F_Handle,'x',x)
setappdata(F_Handle,'y1',y1)
setappdata(F_Handle,'y2',y2)
setappdata(F_Handle,'y3',y3)
setappdata(F_Handle,'y4',y4)
function LOCALPlot(varargin)
F_Handle = varargin{3};
PlotNum = varargin{4};
x = getappdata(F_Handle,'x');
y1= getappdata(F_Handle,'y1');
y2= getappdata(F_Handle,'y2');
y3= getappdata(F_Handle,'y3');
y4= getappdata(F_Handle,'y4');
if PlotNum == 1
plot(x,y1)
elseif PlotNum == 2
plot(x,y2)
elseif PlotNum == 3
plot(x,y3)
elseif PlotNum == 4
plot(x,y4)
end
xlim([1 10])
ylim([0 1])
No comments:
Post a Comment