[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:

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:

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.

  1. · When you create a jagged array you declare the number of rows in your array. Each row will hold an array that will be on any length. Before filling the values in the inner arrays you must declare them.

Leave a Reply

%d bloggers like this: