Monthly Archives: October 2012

[matlab] getting and setting value in other form/figure/gui

I often using need to do this in other visual programming (e.g. Visual Basic, Delphi), i.e. getting and setting value from other forms. LearningĀ  Matlab gui programming, I was a bit puzzled on how to do this because obviously we can’t do this: form2.edit1 in our Matlab’s m-script. After some searching, I finally got it, so, here I share what I found to Matlab’s newbie like me.
To follow, we will create two figures via GUIDE named form_1.fig and form_2.fig. Here’s the design for form_1
form_1 design
There are two edit-text named edtValForm2 and edtNewValue. Two pushbutton named pbGetVal and pbSetVal.
This is the design of form_2
form_2 design
There’s only one edit text here, named edit1 and set the ‘String‘ property value to “This is the default value in Form #2“. No coding needed in this form_2 because we will change the edit text via form_1

Getting value from other form

You should run form_2 first.
View pbGetVal callback on your m-script editor and enter the code below:

% find form #2 (form_2) handle. Use findobj! We need to find a figure/form that has Name: form_2
hf2=findobj(0, 'Type', 'figure', 'Name', 'form_2');
% check if hf2 is true handle
if (ishandle(hf2))
    % hf2 is real handle, form_2 is already opened
    % now get a handle of edit1 in form #2
    edt1_Form2=findobj(hf2, 'Tag', 'edit1');
    % get the String value from edit1 in Form #2
    val_Form2=get(edt1_Form2, 'String');
    % show the value in our form (form_1)
    set(handles.edtValForm2, 'String', val_Form2);
else
    % hf2 is not a handle, means: the form_2 has not been opened yet
    warndlg('Form #2 not found! Maybe it''s not opened yet?');
end

The comments on the code should enough to explain what happened in the code

Setting value into other form

View pbSetVal callback and enter the code below:

% find form #2 (form_2) handle
hf2=findobj(0, 'Type', 'figure', 'Name', 'form_2');
% check if hf2 is true handle
if (ishandle(hf2))
    % hf2 is real handle, form_2 is already opened
    % now get a handle of edit1 in form #2
    edt1_Form2=findobj(hf2, 'Tag', 'edit1');
    % get the String value from our form
    val_Form1=get(handles.edtNewValue, 'String');
    % set the new value into Form #2 (form_2)
    set(edt1_Form2, 'String', val_Form1);
else
    % hf2 is not a handle, means: the form_2 has not been opened yet
    warndlg('Form #2 not found! Maybe it''s not opened yet?');
end

The codes also had comments to explain what happened.
Now run form_1, try clicking on pbGetVal and pbSetVal, you’ll see the effect.
form_1 and fomr_2 running together

Although I gave an example on edit text, this technique also work if you want to change other gui component value such as uitable ‘Data’ property or even plotting into axes in other form.

DeAdfly

So, I’ve come to a place(=network) where you can’t access Adf.ly website. I download TV shows a lot, since a lot of TV shows download link is masked/hidden with adf.ly service, I can’t access the real download page because it isn’t redirected properly. Finally I decided to create a PHP script that decode this adf.ly URL into it’s real URL.
Here’s the final result (I know it’s ugly, but it works.. šŸ˜› ):
http://demo.ahowto.net/deadfly/

de-adf.ly
Although the site is created using PHP, I will not share the script. Why? Because a lot of webmasters (including me) gaining revenue from adf.ly, I don’t want this kind of script kill those revenue source. It’s for personal use only.