MATLAB Answers


How do I toggle images in the GUI

Asked by Jeffrey
on 22 Aug 2013

I am a current student and am relatively new to MATLAB. I am currently using an accelerometer as controller.

What I am trying to do is to replace an image when the accelerometer reads a value greater then a set threshold and return to the original image when it is not. This is what I currently have in a while loop

if handles.thresholdValue <= handles.gxFiltdata;

As of right now the image starts off black and turns red and stays red regardless of whether or not the accelerometer reads above or below the threshold set


1 Answer

Answer by Image Analyst
on 22 Aug 2013

Put this before your if statement

fprintf('thresh=%f, gxFilt=%f\n', handles.thresholdValue, handles.gxFiltdata)

What does it show in the command window? Next, go here and watch it:


Image Analyst
on 25 Aug 2013

It is changing somehow, somewhere. But I don't know when/where. I suggest you search for all "handles.thresholdValue" in your code and see where an equal sign follows that, meaning that you are setting it to some new value.

on 26 Aug 2013

Even when I change the if statement to

'if handles.gxdata <= .5;'

The image stays put at red. Is here something wrong with what is inside the loop itself?

Image Analyst
on 26 Aug 2013

Is this an image and you're wanting to change the image to pure red if the image value is less than the threshold and to black if it's above the threshold? If so, you can just do (untested):

binaryImage = (handles.gxFiltdata >= handles.thresholdValue)
myColormap = [1,0,0;0,0,0];

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!