The End of Blinkdagger? . . . . Possibly

Hello Everyone. As many of you have probably noticed, Blinkdagger has not been posting with as much regularity. I would like to announce that this is probably going to be one of the last posts you will see at Blinkdagger. There are no plans to continue posting MATLAB tutorials in the immediate future.

At the time of this post, there are over 800 RSS subscribers, and over 2500 visitors a day on average. I started this website with Daniel from nothing and have developed it to an impressive level. A lot of you are probably wondering “Why?” I will do the best I can to explain the circumstances that have led to this decision.

Daniel Sutoyo Accepts a Position at The MathWorks

If there’s one good thing that has emerged from this website, it’s the fact that Daniel was able to obtain a position at The MathWorks. Unfortunately, as soon as Daniel became an employee of The MathWorks, there was a conflict of interest in being affiliated with this website. Originally, no one knew what to expect from this union. When we were going through the interview process, we were told that we would be able to maintain this blog while simultaneously working at The MathWorks. As it turns out, things would not be so simple.

After several meetings with The MathWorks legal team, it was determined that Daniel would be unable to contribute anything MATLAB related to this website. Thus, that essentially ended Daniel’s participation here at Blinkdagger as everything on this website is basically MATLAB related. Since this site took a great deal of effort to maintain, we found it difficult to continue on since one half of the team was removed from the equation. We had some big plans moving forward, but in the end we found that working around the legal ramifications would prove insurmountable, and that our time would be better spent on other projects.

The MathWorks Support - Or Lack of

One of the things that bothered me is that The MathWorks was not very supportive of the efforts over here at Blinkdagger. Over the course of this website’s life, the site provided over 100+ informative tutorials on how to use MATLAB to create GUIs, how to create plots, use particular functions, and various other tips and tricks on learning MATLAB.

I believe that Blinkdagger has created and maintained a valuable MATLAB resource; this is corroborated with hundreds if not thousands of positive feedback through the many comments that are received on a daily basis. Many months ago, I had queried The MathWorks on obtaining a personal license for MATLAB. Since this website provides many useful tutorials, it did not seem like a stretch by any means to make such a request. I was basically told that The MathWorks would be unable to provide such a license due to some legal restriction. If they were to provide such a license, they would then have to start monitoring the blog content, etc. To be honest, I found this quite frustrating. The way I saw it, it would be a win/win situation for both parties at hand. Unfortunately, the request was never granted.

Additionally, I felt that The MathWorks could have done a better job promoting and assisting websites like this one in order to cultivate an environment wherein more bloggers would be compelled to contribute to the MATLAB community. The MathWorks have created a great internal community at MATLAB central, but the potential for growth could be so much more and over a broader spectrum if they provided more support and incentive. In my opinion, it would be to the advantage of The MathWorks if there were more blogs like Blinkdagger out there, helping other users learn how to use this great tool. In fact, as of now, Blinkdagger is probably the largest external MATLAB website/blog. While The MathWorks have some internal blogs, those are homegrown and provide an entirely different flavor. The potential for external blogs is, in my opinion, much greater and provides a different platform wherein MATLAB can thrive and flourish.

Final Thanks

Finally, I would like to thank everyone who helped contribute to this site. Specifically, I would like to thank Rob Slazas, Sol Lederman, and Zane Montgomery, for their help throughout the last two years. Finally, I would like to thank everyone who was kind enough to leave an encouraging comment. The comments Blinkdagger received were the best part about maintaining this website, as it was concrete proof and positive reinforcement that Blinkdagger was helping others understand and use MATLAB. The reason Blinkdagger continued on for so long was because of the kind comments that were received.

In Closing

This blog has been something that I have poured a lot of work into, and it is difficult to leave it as is. If anyone is serious in contributing to the blog or wants to see Blinkdagger continue, please leave a comment below or send a memo through the contact form. For now, there will be no more tutorials, but the website will continue to be active until the end of the hosting contract (which may or may not be renewed).

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB - Global Variables

Matlab LogoHow do global variables differ from regular (local) variables?

Each function in MATLAB contains a set of variables specific to that function.  Even in the same .m file, you don’t have (direct) access to variables created in other functions within the file.  Global variables give you the ability to create/change a variable in one function and have that updated variable accessible elsewhere.  This post will discuss two methods for handling (no pun intended) global variables, one of which is perfectly integrated into Graphical User Interfaces (GUIs).

METHOD 1: global VARIABLE

The first (non-GUI) way to create a global variable is to use the function ‘global’.  Create the global variables X, Y, and Z with the command:

global  X Y Z

The ‘global’ function needs to be called in each separate function (usually in the beginning) where the variables will be called.   Stylistically, the variable names are usually longer names and all in CAPS to indicate global variables within the functions.  The documented example in the MATLAB helps shows this pretty well:

function tic
global TICTOC %define/incorporate global variable at start of function
TICTOC = clock;

function t = toc
global TICTOC %accesses variable TICTOC (or creates it if TICTOC is undefined)
if nargout < 1
    elapsed_time = etime(clock, TICTOC)
else
    t = etime(clock, TICTOC);
end

Many hard-core coders prefer to avoid using ‘global’ except for constants.  The reason behind this is because it’s generally considered poor form to lock up a variable name (See Steve L’s comment below for another reason!).  While this won’t matter for smaller programs and functions, when the files get to be many hundreds (or thousands or millions) of lines long, it can be very difficult to keep track of all of the global variables and to remember to call all the necessary variables at the start of each function.  The great thing about GUIs is that they already have a built-in global structure to deal with all of your global variables: the handles.  The handles structure is an input (and therefore accessible) to every function in the GUI, making it perfectly capable doing everything the ‘global’ command can.  In fact, you shouldn’t ever have to use ‘global’ command when designing a GUI because the handles structure does the job so well.  GUIs and ‘global’ don’t mix kids!

METHOD 2: handles.variable

As you may have seen from many of the blinkdagger GUI tutorials, the handles structure is an extremely useful method to manipulate GUI boxes/buttons/tools.  But the tool data are all just stored variables that can be accessed anywhere within the GUI (aka global variables!).  Since we don’t need to edit any ‘property’ of the handles structure (e.g. handles.static_text, ‘String’), we don’t need to use the ‘get’/’set’ commands.  Creating the global variable is as easy as saying:

handles.x = 42;
%And of course, don't forget to update your handles structure:
guidata(hObject, handles);

handles.x is now an independent variable and note that it has no relation to the local variable x.

x = 43;

is a completely valid command in the same function that would not overwrite your global variable ‘handles.x’.

Remember, these variables can range from constants (e.g. 12) to strings (e.g. ‘Hello World’) to structures, cells, and arrays of constants/strings.

Hopefully you can see the usefullness of global variables and will use them (properly!) in your coding adventures.

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MMM #34 Winner, Sander Land!!!

MMM #32 Winner

mmmwin.jpg

The winner for the last MMM at Blinkdagger is Sander Land. A lot of you mistook this problem for the classic bridge/flashlight problem, but it was actually a little more complicated than that one. You can view the problem statement here. As mentioned previously, this is the last MMM to be conducted here at blinkdagger. We appreciate everyone for participating in this contest. It was a lot of fun while it lasted and we hope that there will be many new puzzles to solve over at wildaboutmath.com! Good luck to all!

The Answer by Sander Land

First, simply consider a general strategy for crossing the bridge.
It is clear that
- both Daniel and Quan need to use the bike at some point
- Sol needs to be the one to transfer the bike from one of them to the other.

This leaves a strategy like:
- Daniel takes the bike from the start to point x1, dismounting, and
continues to walk.
- Sol walks to point x1, and takes the bike back to point x2, and
walks to the end point.
- Quan walks to point x2, picks up the bike, and bikes to the end.

Taking into account the fact that any solution which has
- one of them arrive earlier than the others or
- one of them waiting at any time (including t=0)
can be improved, and normalizing the length of the bridge to 1, the
equations for this are:

D: x1 + 15*(1-x1) = T
Q: 8*x2 + (1-x2) = T
S: 3 + 4*(x1-x2) = T (i.e. walking the entire length, and
both walking and biking x1-x2)

Which matlab can help us solve:
>> D=15; Q=8; S=3;
>> R=[-D -1 -S]‘
>> A=[-(D-1) 0 -1 ; 0 (Q-1) -1 ; (S+1) -(S+1) -1]
>> A\R
0.7692 => x1 is at 77% of the bridge length
0.4615 => x2 is at 46% of the bridge length
4.2308 => They can cross the bridge in about 4 minutes and 14 seconds.

The other possible strategy (Quan starts on the bike) can also be
tried by simply switching Daniel and Quan’s velocities. This results
in the same shortest time.
>> D=8; Q=15; S=3;
0.5385
0.2308
4.2308

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

Monday Math Madness #34: Crossing a Bridge

mmm
Hey everyone, this is the last iteration of Monday Math Madness at Blinkdagger. We hope you guys all had fun for the past year or so. Good luck to all! Without further ado . . . here is the final problem!

The Problem Statement

Daniel, Quan, and Sol need to cross a bridge. Daniel can cross the bridge in 15 minutes, Quan can cross in 8 minutes, and Sol can cross in 3 minutes. There is also a bicycle available and any person can cross the bridge in 1 minute with the bicycle. What is the shortest time that all three people can get across the bridge?

Assume that each person travels at his own constant rate. In addition, assume that mounting and dismounting the bicycle is instantaneous.

Continue Reading »

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

MATLAB GUI Tutorial - UITABLE Part 2, How To Access Table Data

Matlab Logo Last time, we learned how to display data onto a table. This time, we’re going to learn how to work with table data within a GUI framework. For example, say you wanted to take the contents of the table manipulate the data. This tutorial will explain how to do that, and much more.

MATLAB UITABLE Tutorial 2

Contents

Accessing Table Data within GUI Callbacks

Let’s say you have the following GUI:

MATLAB UITABLE Tutorial 2

For simplicity sake, let’s assume that you would like to create a button that will add 3 to each of the entries of the table when the button is pressed. How would you go about doing this? It’s actually quite straightforward. Let’s take a look at the callback for the add button:

function add_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to add_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%get the table data
tableData = get(handles.uitable1,'data');

%add 3 to the table
tableData = tableData + 3;

%update the table
set(handles.uitable1,'data',tableData);

So now, when I press the “Add 3″ button, it adds 3 to the table! This is just a simple example to show how to extract the data from the UITABLE, and to perform an operation on it.

MATLAB UITABLE Tutorial 2

Accessing only the Selected Data

Now, let’s up the difficulty level a bit. Let’s say you selected a couple of cells that you want to sum, as shown in the image below (You can hold onto the CTRL button while clicking on individual cells to select multiple cells).

MATLAB UITABLE Tutorial 2

How would you go about doing this? Read on and all will be revealed.

Enabling CellSelectionCallback

The first thing we need to do is to enable the Cell Selection Callback. But first, why are we doing this? Enabling this callback will allow us to keep track of what cells are being selected on the table. You can do this by bringing up the Property Inspector for the UITABLE, and then clicking the following icon as shown in the image below.

MATLAB UITABLE Tutorial 2

If you did it correctly, your m-file should have been updated to include the following:

% --- Executes when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)

In addition, the CellSelectionCallback field should now be populated as shown in the image below:

MATLAB UITABLE Tutorial 2

Adding the Necessary Code

First, let’s create and initialize a variable to hold the table cell selection information. We will call this handles.selectedCells, and initialize it in the opening function.

% --- Executes just before uitable_tutorial_02 is made visible.
function uitable_tutorial_02_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to uitable_tutorial_02 (see VARARGIN)

%initialize this variable
handles.selectedCells = [];

Next, we go to the uitable1_CellSelectionCallback, which is the callback that we just enabled.

% --- Executes when selected cell(s) is changed in uitable1.
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitable1 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)

%every time the cell selection changes, we update this data
%eventdata stores the indices of the selected cells
handles.selectedCells = eventdata.Indices;

%update the gui data
guidata(hObject, handles);

Adding the Selected Numbers Together

First, we’re going to add another button and a static text component to display the sum. The modified GUI looks like this:

MATLAB UITABLE Tutorial 2

Now, we need to write the callback for the button we just added:

% --- Executes on button press in sumNumbers_pushbutton.
function sumNumbers_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to sumNumbers_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%get the number of rows and columns
[rows,columns] = size(handles.selectedCells);

%get the data from the UITABLE
tableData = get(handles.uitable1,'data');

%initialize the sum
sum = 0;

%loop through each selected cell and keep a running sum
%can anyone thing of a better way to do this?
for x=1:rows
    sum = sum +tableData(tableIndices(x,1),tableIndices(x,2));
end

%display the sum on the GUI
set(handles.sum_text,'String',num2str(sum))

And there you have it, now you can select any number of cells, and then sum up the value of the contents!

MATLAB UITABLE Tutorial 2

Next Time

Next time, we’re going to talk about some of the cool features of the UITABLE that we have not yet discussed, including different data types within the UITABLE.

Links and Downloads

Download Source Files
The MathWorks Documentation for UITABLE
Cool Things You can do with UITABLE
Doug’s Video on UITABLE

delicious bookmark StumbleUpon technorati Digg reddit icon rss icon

Next »