Tag Archives: GUIDE

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

Matlab variable sharing (global variable and such and such…)

Coming from another programming language (Delphi/C#), the one puzzled me about programming in Matlab is how do I share my variable just like I programmed desktop application using Delphi or C#.

After reading the documentation and a lot of forum browsing, finally I made up my mind and conclude my own “best-practice” when I want to share (global) variables in Matlab. (This is applied when we are developing a Matlab GUI via GUIDE).

Sharing variables between functions in one Form/GUI

  • use guidata and handles.

create a global variable that will be known by all functions inside a form/GUI

recall/access that global variable within other function (still in the same form)

caveat: all global variables is destroyed when you close the Form/GUI


Sharing variables between multiple Forms/GUI but in one application

  • use setappdata and getappdata

create a global variable

accessing it

checking it (if it’s already set or not)

deleting it

caveat: variables is gone when you close Matlab; you can’t see the variables within Matlab Workspace


The highest level: sharing between multiple Forms/GUI and also with your Matlab Workspace/Command Window

  • use assignin and evalin



See the variables in your Matlab Workspace so you can analyze the variable further (maybe via Variable editor or directly via Command Window)

matlab workspace window

Matlab workspace window showing global variable

caveat: variables is gone when Matlab is closed

If you want to create variables that will not gone even after Matlab closed, you can use save and load command. Checkout Matlab Help page for further documentation about save and load.


Detecting installed webcam via command line/m script file

On my previous Matlab tips, I already show you how to create a GUI (GUIDE)  to capture webcam picture. I show you how to easily detect installed and supported video format using imaqtool command. This time I’ll show you how to detect installed webcam without calling imaqtool window. This would be useful if you want to automatically detect installed webcam without needing user interaction/input.

Your Matlab still need Image Acquisition Toolbox though and your webcam should be connected before you open Matlab.

  • First step is to detect installed webcam drivers on in your Matlab, because it could be different on different platform (Windows/Linux/Mac).

Variable adaptors now will contain installed drivers on your Matlab. Example output would be (InstalledAdaptors is a cell):

  • Now we know that my Matlab knows ‘matrox’ and ‘winvideo’ driver. Our next step would be detecting is there any webcam attached to those drivers. Use this command :

  • For detecting how many installed webcam that detected as ‘matrox’ we can use:

  • Oh, as you can see, there’s no webcam detected as ‘matrox’ so we are certainly will not use this driver. How about ‘winvideo’ webcam, let’s check:

  • Yup, there’s one installed and active there, let’s check further to know how to command Matlab to use this webcam:

  • Now you got what command that you should use to activate the webcam [videoinput(‘winvideo’, 1)]. How about the supported image/video formats supported by this webcam? Use this command:

Oh, there are 5 types of supported image/video. You can choose one.

Your information is now complete, you know which webcam is active, what command that need to be called to activate the webcam and which format available for this webcam. You can begin to capture the image/video from webcam using Matlab.

matlab 2011a logo

Tested on Matlab 2011a on Windows 7 64 bit within Asus Notebook A43SV

Preview and capture webcam into an axes

My first Matlab tips. We will create a GUI form that will preview and capture picture from webcam connected to your computer. Webcam preview and captured image will be shown on an axes.

  • Create a new blank GUI using Matlab’s GUIDE (File –> New –> GUI –> Blank GUI). Put one panel, two pushbutton and one axes. You can arrange them like this:
New form from Matlab's GUIDE

Arrange the component like this (just for example)

  • Tag the axes as axPreview, first pushbutton as pbPreview and second pushbutton as pbCapture.
  • When user click on pbPreview image form webcam will be shown on axPreview. pbCapture will capture the image, show it on axes and save it as an image file.
  • you need to know how’s Matlab detected your webcam. Open Image Acquisition Toolbox. From Matlab’s Command Window, type this: imaqtool  OR you can follow this screenshot to access it via Matlab’s start menu
matlab access imaqtool via menu

access imaqtool via start-menu

  • Make sure your webcam is connected in your computer before Matlab run! If not, Matlab might not detect your webcam
  • imaqtool window will be shown. See the screenshot (a part of imaqtool form):
matlab supported webcam mode

Matlab detected my webcam

  • From the screenshot, my webcam is detected as winvideo-1 and Matlab also lists all supported mode that my webcam has. I’ll use YUY2_176x144 mode (the 176×144 number is obviously the image size)
  • Put these codes on your pbPreview_Callback (it’s not quite pretty colored, GeSHi doesn’t support Matlab .m file language yet). Read the code’s comment for the explanation line by line

  • as for the pbCapture_Callback. Most of codes explanation are the same as pbPreview_Callback

  • Example result:
matlab preview and capture webcam

Example form running our Matlab codes

Things to remember:

  • connect your webcam before running Matlab
  • put a pause a few seconds before capturing image, without this you’ll capture black/dark image!
  • choose rgb as ReturnedColorspace before capturing image

UPDATE: since there are people that having difficulties implementing the code, you can download this .zip file so you can quickly try it on your own Matlab. (Please remember that this code is created using Matlab 2011a GUIDE, I don’t guarantee it will work on earlier version)

Download MATLAB demo codes to capture image from webcam

UPDATE 2 : In order to make the codes work on latest MATLAB you should make “vid” become a global variable. Here’s how:
on pbPreview_Callback, after preview(vid, hImage);, add these:

on pbCapture_Callback, replace vid = videoinput(‘winvideo’, 1, ‘YUY2_176x144’); with these:

Now it should work fine on latest MATLAB, tested on MATLAB 2015b.