Rotating an entire axes in Matlab Guide - matlab

I am currently trying to rotate an entire axes named axes1.
imr=imrotate(img,30);
axes(this.gui_h.axes1);
imshow(imr,'Parent',this.gui_h.axes1);
The code above initiates a rotation of 30 degrees. However, the image is rotated but not the entire axes1. I have tested guide tools such as the rotate3D, however rotate3D does not work successfully for 2d images. I have also tried set(handles.axes1,'Rotation',-25); , which has no effect. It simply overlooks the statement and continues with the other tasks. Is there a way to rotate the entire axes?

You can use the view functin to rotate the axes.
imshow('Jupiter_New_Horizons.jpg')
xlabel('X axis')
ylabel('Y axis')
camzoom(.8)
% Rotate the axes changing the Azimuth value
for i=0:-3:-180
view([i 90]);
pause(.3)
end
This also applies to standard plots:
t=0:.1:2*pi;
x=sin(t)
plot(t,x);
grid minor
xlabel('X axis')
ylabel('Y axis')
camzoom(.8)
for i=0:-3:-180
view([i 90]);
pause(.3)
end
Edit following the comments
I've created a simple GUI with two axes and two pushbutton with the following tag
axes #1: axes1
axes #2: axes2
pushbutton #1: pushbutton1
pushbutton #2: pushbutton2
The callback of pushbutton1 loads an image in the axes1, the turns the axes.
The callback of pushbutton2 plots a curve in the axes2, the turns the axes.
The GUI works properly, the axers rotates as expected.
This is the .m of the GUI; you can test it creating the GUI and using the tag as specified above.
function varargout = fbdfi(varargin)
% FBDFI MATLAB code for fbdfi.fig
% FBDFI, by itself, creates a new FBDFI or raises the existing
% singleton*.
%
% H = FBDFI returns the handle to a new FBDFI or the handle to
% the existing singleton*.
%
% FBDFI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FBDFI.M with the given input arguments.
%
% FBDFI('Property','Value',...) creates a new FBDFI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before fbdfi_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to fbdfi_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help fbdfi
% Last Modified by GUIDE v2.5 26-Feb-2017 20:56:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #fbdfi_OpeningFcn, ...
'gui_OutputFcn', #fbdfi_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before fbdfi is made visible.
function fbdfi_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to fbdfi (see VARARGIN)
% Choose default command line output for fbdfi
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes fbdfi wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = fbdfi_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imshow('Jupiter_New_Horizons.jpg','parent',handles.axes1)
xlabel(handles.axes1,'X axis')
ylabel(handles.axes1,'Y axis')
camzoom(handles.axes1,.8)
for i=0:-10:-180
view(handles.axes1,[i 90]);
pause(.3)
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
t=0:.1:2*pi;
x=sin(t)
plot(handles.axes2,t,x);
grid minor
xlabel(handles.axes2,'X axis')
ylabel(handles.axes2,'Y axis')
camzoom(handles.axes2,.8)
for i=0:-10:-180
view(handles.axes2,[i 90]);
pause(.3)
end
Hope this helps,
Qapla'

Related

Matlab Attempt to reference field of non-structure array

I have 3 figures
1- principle contain 2 buttons
2- figure to load one image and diplay it on an axe
3- figure to load two images and diplay it on axes
when i run the figure 3 or 2 all alone everything is fine and the images got displayed.
THE PROBLEM when i run the prgrm from principle.fig and open the 2nd figure or the 3rd an error popup
Attempt to reference field of non-structure array.
Error in untitled3>pere_Callback (line 91)
axes(handles.axes1);
my code
function varargout = untitled3(varargin)
% UNTITLED3 MATLAB code for untitled3.fig
% UNTITLED3, by itself, creates a new UNTITLED3 or raises the existing
% singleton*.
%
% H = UNTITLED3 returns the handle to a new UNTITLED3 or the handle to
% the existing singleton*.
%
% UNTITLED3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED3.M with the given input arguments.
%
% UNTITLED3('Property','Value',...) creates a new UNTITLED3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled3_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled3
% Last Modified by GUIDE v2.5 01-May-2017 17:19:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #untitled3_OpeningFcn, ...
'gui_OutputFcn', #untitled3_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled3 is made visible.
function untitled3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled3 (see VARARGIN)
% Choose default command line output for untitled3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled3_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pere.
function pere_Callback(hObject, eventdata, handles)
% hObject handle to pere (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im im2
[path,user_conce] = imgetfile();
if user_conce
sprintf('error');
return
end
im=imread(path);
im2 = im; %backup
im = im2double(im);
im = rgb2gray(im);
axes(handles.axes1);
imshow(im);
% --- Executes on button press in fils.
function fils_Callback(hObject, eventdata, handles)
% hObject handle to fils (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im3 im4
[path,user_conce] = imgetfile();
if user_conce
sprintf('error');
return
end
im3=imread(path);
im4 = im3; %backup
im3 = im2double(im3);
im3 = rgb2gray(im3);
axes(handles.axes3);
imshow(im3);
% --- Executes on button press in tester.
function tester_Callback(hObject, eventdata, handles)
% hObject handle to tester (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

Connecting MATLAB GUI to .m file

I have a m file, which chops the signal and applies filter according to the cut off frequency(Fc).
M file:
classdef Container < handle
properties
segments = struct('signal', {}, 'time', {},'ref',{}); %empty structure with correct fields
end
methods
function this = addsignal(this, signal, time,fc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%chopping of the signals%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
interval = diff(time);
[~, locations] = findpeaks(interval,'THRESHOLD',0.7);
edges = [0; locations; numel(signal)+1];
newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1));
%this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
for edgeidx = 1 : numel(edges) - 1
newsegments(edgeidx).signal = signal(edges(edgeidx)+1 : edges(edgeidx+1)-1);
newsegments(edgeidx).time = time(edges(edgeidx)+1 : edges(edgeidx+1)-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%filtering%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = ltiFilter.PT1(); % another class which has filters
f.Ts = mean(diff(time));
f.fc = fc; % i want to set this value from the slider%%%%%
f.zeroPhaseShift = 1;
for i = 1:length(newsegments)
newsegments(i).ref = f.eval(newsegments(i).signal,newsegments(i).signal(1)); % application of the filter.
newsegments(i).ref = newsegments(i).ref';
end
this.segments = [this.segments; newsegments];
end
end
end
I created a GUI which has a plot and a slider(for cut off frequcy) which is shown in code as f.fc
when i created the GUI, Matlab automatically created a Code for me(i must say, I din't understand that much)
GUI code:
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% GUI, by itself, creates a new GUI or raises the existing
% singleton*.
%
% H = GUI returns the handle to a new GUI or the handle to
% the existing singleton*.
%
% GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI.M with the given input arguments.
%
% GUI('Property','Value',...) creates a new GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI
% Last Modified by GUIDE v2.5 15-Jul-2016 09:37:09
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #GUI_OpeningFcn, ...
'gui_OutputFcn', #GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
What i want to do is, i want to connect the GUI to my m script, when user sildes the slider . it should show the change in graph automatically and when he clicks on apply. the value of slider should be taken and should be available in my m file.
Any leads will be helpful.
From code sample pasted, I'm assuming you are using MATLAB GUIDE. Let's assume name of slider control is "slider1".
Add a callback function for "slider" using GUIDE.
It will create a function "function slider1_Callback(hObject, eventdata, handles)" in your code.
Now, to get value selected from slider movement use "get" function with "hObject".
E.g. SliderVal=get(hObject,'Value');
Now if you want to know value of slider selection from other callbacks (such as Apply button)
use handles structure.
E.g.: SliderVal=get(handles.slider1,'Value');
Based on slider value, received you need to re-draw plot area.
I hope this helps as clue you are expecting.
Edit1:
For followup comment, how to get data from other M-file:
This will be very tricky. Because, you need to know handle of slider control in other M-file.
One of the ways would be to get handle of figure first.
Set "HandleVisibility" property of GUI figure (via GUIDE) to "ON".
Call "figures = get(0,'Children');" from M-script to get list of all open figures. This will give vector of figure handles.
Scan through list of children and get handle of your application. (This can be done via using property get(figures(1),'Name')).
Let's assume you found that handle, repeat same process again to get children from it. get(figHandle,'Children').
Scan through children and find slider control handle similar to approach as in step 3.
Now you have access to control and it's data.
I hope you understood it.

need help about opening dicom file in matlab

i got black image when opening dicom file in matlab
my code for opening dicom files
[real_image,real_path] = uigetfile( ...
{'*.*;','File Dicom (*.dcm)';},...
'Open Image');
if ~isequal(real_image,0)
handles.image = dicomread(fullfile(real_path,real_image));
guidata(hObject,handles);
axes(handles.axes1);
imshow(handles.image);
set(handles.text5,'String',real_image);
set(handles.text6,'String',real_path);
[row,column]=size(handles.image);
set(handles.text7,'String',row);
set(handles.text8,'String',column);
else
return;
end
I've tested your code by creating a GUI (R2012b) and inserting the code in pushbutton callback and it works fine.
So I can see two possibilities:
- the dcom image you are loading in somehow corrupted
- the dcom image is good and it is a probelm of contrast
I'll go for the second possibility.
I've used, as examples, the dcom images "CARDIX" downloaded from DICOM sample image sets.
As one of these images is loaded, it appears to be black and the axes's clim is [0 65535].
If you set the clim range to, respectively, the minimum and maximum values of the dcom image data value, you can start "seeing something".
To further enhance the visibility of the image you can use the imcontrast tool.
In the following you can find the .m file of the GUI: the imcontrast tool can be run through a pushbutton.
function varargout = adjust_contrast(varargin)
% ADJUST_CONTRAST MATLAB code for adjust_contrast.fig
% ADJUST_CONTRAST, by itself, creates a new ADJUST_CONTRAST or raises the existing
% singleton*.
%
% H = ADJUST_CONTRAST returns the handle to a new ADJUST_CONTRAST or the handle to
% the existing singleton*.
%
% ADJUST_CONTRAST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ADJUST_CONTRAST.M with the given input arguments.
%
% ADJUST_CONTRAST('Property','Value',...) creates a new ADJUST_CONTRAST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before adjust_contrast_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to adjust_contrast_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help adjust_contrast
% Last Modified by GUIDE v2.5 26-Dec-2015 10:44:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #adjust_contrast_OpeningFcn, ...
'gui_OutputFcn', #adjust_contrast_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before adjust_contrast is made visible.
function adjust_contrast_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to adjust_contrast (see VARARGIN)
% Choose default command line output for adjust_contrast
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes adjust_contrast wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = adjust_contrast_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[real_image,real_path] = uigetfile( ...
{'*.dcm;','File Dicom (*.dcm)';},...
'Open Image');
if ~isequal(real_image,0)
handles.image = dicomread(fullfile(real_path,real_image));
guidata(hObject,handles);
axes(handles.axes1);
imshow(handles.image);
set(handles.text5,'String',real_image);
set(handles.text6,'String',real_path);
[row,column]=size(handles.image);
set(handles.text7,'String',row);
set(handles.text8,'String',column);
else
return;
end
% Set the "Clim" scale to the [min max] image values
set(gca,'clim',[min(min(handles.image)) max(max(handles.image))])
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imcontrast
In the following picture you can see:
- the "black" image as it appears as loaded
- the first enhacement following the updating of the clim range
- the enhancements obtained "working" with the imcontrast tool
Notice: you can download a DCOMViewer from MATLAB Central.
Hope this helps.

Error using guidata .H must be the handle to a figure or figure descendent

i am pretty new in matlab and I am facing some problems.
really appreciate if someone could help me out. I am currently doing a project on face detection and I would want to allow users to on the webcam whereby he/she will have a face detected. Once the webcam is off, the function will be shut down. But the problem is I am unable to shut down the program but only able to do so with the help of ctrl + c, besides that instead of viewing it in the axes1, it opens up in a video player. My code is as attached:
function varargout = face_tracking(varargin)
% FACE_TRACKING MATLAB code for face_tracking.fig
% FACE_TRACKING, by itself, creates a new FACE_TRACKING or raises the existing
% singleton*.
%
% H = FACE_TRACKING returns the handle to a new FACE_TRACKING or the handle to
% the existing singleton*.
%
% FACE_TRACKING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACE_TRACKING.M with the given input arguments.
%
% FACE_TRACKING('Property','Value',...) creates a new FACE_TRACKING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before face_tracking_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to face_tracking_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help face_tracking
% Last Modified by GUIDE v2.5 28-Jan-2015 00:39:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #face_tracking_OpeningFcn, ...
'gui_OutputFcn', #face_tracking_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before face_tracking is made visible.
function face_tracking_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to face_tracking (see VARARGIN)
% Choose default command line output for face_tracking
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes face_tracking wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = face_tracking_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes when figure1 is resized.
function figure1_ResizeFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in cameraon.
function cameraon_Callback(hObject, eventdata, handles)
% hObject handle to cameraon (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
faceDetector = vision.CascadeObjectDetector();
obj = imaq.VideoDevice('winvideo', 1, 'MJPG_320x240', ...
'ROI', [1 1 320 240]);
handles.vid=obj;
videoFrame = step(obj);
%Get a bounding box around the face
bbox = step(faceDetector, videoFrame);
%Check if something was detected, otherwise exit
if numel(bbox) == 0
errordlg('Face not detected. Please try again.');
end
[hueChannel,~,~] = rgb2hsv(videoFrame);
noseDetector = vision.CascadeObjectDetector('Nose');
faceImage = imcrop(videoFrame,bbox);
noseBBox = step(noseDetector,faceImage);
% The nose bounding box is defined relative to the cropped face image.
% Adjust the nose bounding box so that it is relative to the original video
% frame.
noseBBox(1:2) = noseBBox(1:2) + bbox(1:2);
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
initializeObject(tracker, hueChannel, noseBBox);
ROI = get(obj,'ROI');
videoSize = [ROI(3) ROI(4)];
VideoPlayer = vision.VideoPlayer('Position',[300 300 videoSize(1:2)+30]);
% Track the face over successive video frames until the video is finished.
%You could set here a finite number of frames to capture
nFrames=0;
while (nFrames<100)
% Extract the next video frame
videoFrame = step(obj);
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel(:,:,1));
% Insert a bounding box around the object being tracked
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
% Display the annotated video frame using the video player object
step(VideoPlayer, videoOut);
nFrames=nFrames+1;
end
guidata(handles,hObject);
% Release resources
release(obj);
release(VideoPlayer);
% --- Executes on button press in cameraoff.
function cameraoff_Callback(hObject, eventdata, handles)
% hObject handle to cameraoff (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear handles.vid
delete (hObject);
the error message is stated below:
Error using guidata (line 89)
H must be the handle to a figure or figure descendent.
Error in face_tracking>cameraon_Callback (line 153)
guidata(handles);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in face_tracking (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)face_tracking('cameraon_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
your help is greatly appreciated.
The error occurs in the cameraon_Callback, at the end of your while loop, because of this command:
guidata(handles,hObject);
When you update the data stored in guidata, the first input argument must be the figure with which the data are associated. As the error message says, it has to be a figure or a descendant.
Try changing the above line with the one previously used in the face_tracking_OpeningFcn:
% Update handles structure
guidata(hObject, handles);
That should work.

How do I show different images with radio buttons in matlab GUIDE?

This is all of the code as of right now. The correct image is displayed when you change the selected button in the button group but no image is displayed until you change the selected button. I want the first image to be FloorPlan{1} as it says in axes1_CreateFcn but it is not working. The axes are simply not there until you change the button.
function varargout = selectfloorplan(varargin)
% SELECTFLOORPLAN MATLAB code for selectfloorplan.fig
% SELECTFLOORPLAN, by itself, creates a new SELECTFLOORPLAN or raises the existing
% singleton*.
%
% H = SELECTFLOORPLAN returns the handle to a new SELECTFLOORPLAN or the handle to
% the existing singleton*.
%
% SELECTFLOORPLAN('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SELECTFLOORPLAN.M with the given input arguments.
%
% SELECTFLOORPLAN('Property','Value',...) creates a new SELECTFLOORPLAN or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before selectfloorplan_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to selectfloorplan_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help selectfloorplan
% Last Modified by GUIDE v2.5 18-Apr-2014 11:16:48
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #selectfloorplan_OpeningFcn, ...
'gui_OutputFcn', #selectfloorplan_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before selectfloorplan is made visible.
function selectfloorplan_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to selectfloorplan (see VARARGIN)
% Choose default command line output for selectfloorplan
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes selectfloorplan wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = selectfloorplan_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
global FloorPlans floorplanselection
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
%Hint: place code in OpeningFcn to populate axes1
imagesc(FloorPlans{1})
floorplanselection=FloorPlans{1};
axis off
axis('image')
colormap(gray)
% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
global FloorPlans floorplanselection
% hObject handle to the selected object in uipanel1
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag')
case 'rad1'
axes(handles.axes1)
imagesc(FloorPlans{1})
floorplanselection=FloorPlans{1};
axis off
axis('image')
colormap(gray)
case 'rad2'
axes(handles.axes1)
imagesc(FloorPlans{2})
floorplanselection=FloorPlans{2};
axis off
axis('image')
colormap(gray)
case 'rad3'
axes(handles.axes1)
imagesc(FloorPlans{3})
floorplanselection=FloorPlans{3};
axis off
axis('image')
colormap(gray)
case 'rad4'
axes(handles.axes1)
imagesc(FloorPlans{4})
floorplanselection=FloorPlans{4};
axis off
axis('image')
colormap(gray)
case 'rad5'
axes(handles.axes1)
imagesc(FloorPlans{5})
floorplanselection=FloorPlans{5};
axis off
axis('image')
colormap(gray)
case 'rad6'
axes(handles.axes1)
imagesc(FloorPlans{6})
floorplanselection=FloorPlans{6};
axis off
axis('image')
colormap(gray)
end
% --- Executes on button press in nectbtn.
function nectbtn_Callback(hObject, eventdata, handles)
% hObject handle to nectbtn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selectrobot
delete(get(hObject, 'parent'));
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mainmenu
delete(get(hObject, 'parent'));
% --- Executes during object creation, after setting all properties.
function uipanel1_CreateFcn(hObject, eventdata, handles)
% hObject handle to uipanel1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
The ID for your graphics objects are stored in the handles structure that GUIDE stores and passes between callbacks. The reason axis1 doesn't work as an input is because it's not defined in the scope of your callback function so the function has no way to address it. This is why your global workaround 'fixes' the problem.
If you substitute handles.axes1 (or whatever your axes object tag is) for axis1 in your axes() calls, it should perform as you're expecting.