Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

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;
              axes(handles.axes9);
              imshow('RED.JPEG');
          else
              axes(handles.axes9);
              imshow('BLACK.JPEG');
          end

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

0 Comments

Jeffrey

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: http://blogs.mathworks.com/videos/category/gui-or-guide/

4 Comments

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.

Jeffrey 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)
imshow(binaryImage);
myColormap = [1,0,0;0,0,0];
colormap(myColormap);
colorbar;
Image Analyst

Contact us