Friday, March 30, 2012

A tip to speed up your MATALB script

A few days ago, my friend sent me a MATLAB script asking for help.
The script had a bug, but he said if the bug is fixed it would take a few hours to run.
I fixed the bug and made some changes, then it took less than 20 seconds to run.
How could this be?

Here's a small tip that can make a huge change in calculation time.

Let's say we are calculating something and updating an array. For example, you start with an array with size (10,1). During the for-loop or while-loop you're adding one more value to it.
So the array size becomes (11,1). This is what happens behind the scene.

Click on the picture to enlarge.

When you ask for one more space that's not ready to use yet, MATLAB has to get that extra space from Windows. (follow the orange arrows). Windows wants to keep elements of an array together, physically. So it goes to the memory to see if there's a room or one more element.

If there is, it gives ok sign to MATLAB (still following the orange arrows), and MATLAB can use it.
If there isn't, Windows has to find a space somewhere else on the memory (not we're following the red arrows). Once that relocation is done, it gives ok sign to MATLAB.

This can be avoided by declaring an empty array before you use it. Like, A=zeros(11,1). Now the space is available and initialized with zeros. When your MATLAB script wants to use that 11th space, it can just go there and use it (blue arrows).

The yellow and red arrows shown above takes much longer time compared to the blue route, and becomes really noticeable when you deal with large array size.

If your array is expanding from (10,1) to (11,1), you won't notice a difference, but if you're expanding from (100000,1) to (100001,1) then (100002,1) and so on, you may end up spending hours instead of minutes.

So guesstimate the array size you'll need and initialize it.
MATLAB is very forgiving in many aspects, but that can slow down your code.




Sunday, March 25, 2012

GUI in MATLAB 8

Wow, it's been 2 weeks since I wrote something for this blog.

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:
% I'm calling this STORAGE because it's about a hidden storage.
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])