Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Urgent!! I need to create a callback function in my GUI and I need HELP!!!

1 visualizzazione (ultimi 30 giorni)
Hey guys,
I am new to creating GUIs in Matlab so I have a question on how exactly to use a callback function. I created a GUI that has a plot and a slider and in the axis callback function I added a code I have to rotate a 2D plot. I then want to be able to use the slider to change the value of the variable that rotates the 2D plot. I think I need to use a callback function to do this but I am very confused how they work. Please help!
Also here is my code:
function varargout = untitled2(varargin) % UNTITLED2 MATLAB code for untitled2.fig % UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing % singleton*. % % H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to % the existing singleton*. % % UNTITLED2('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in UNTITLED2.M with the given input arguments. % % UNTITLED2('Property','Value',...) creates a new UNTITLED2 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled2_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled2_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 untitled2
% Last Modified by GUIDE v2.5 13-Sep-2013 17:16:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled2_OpeningFcn, ...
'gui_OutputFcn', @untitled2_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 untitled2 is made visible.
function untitled2_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 untitled2 (see VARARGIN)
% Choose default command line output for untitled2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled2_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 during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% 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
% define the x- and y-data for the original line we would like to rotate
%handles = guihandles(gcbo)
%wave=get(handles.slider1,'Value')
x=[1; 2; 3; 4];
c=[4 ;5; 67; 6];
y = c;
%close all
% create a matrix of these points, which will be useful in future calculations
v = [x y];
% choose a point which will be the center of rotation
x_center = x(1);
y_center = y(1);
% create a matrix which will be used later in calculations
center = repmat([x_center; y_center], 1, length(x));
% define a 60 degree counter-clockwise rotation matrix
theta=78
R = [cosd(theta) -sind(theta); sind(theta) cosd(theta)];
% do the rotation...
%s = v - center; % shift points in the plane so that the center of rotation is at the origin
%so = R*s; % apply the rotation about the origin
%vo = so + center; % shift again so the origin goes back to the desired center of rotation
% this can be done in one line as:
vo = R*((v - center')') + center;
% pick out the vectors of rotated x- and y-data
x_rotated = vo(1,:);
y_rotated = vo(2,:);
% make a plot
subplot(2,1,1)
plot(x, y, 'k-', x_rotated, y_rotated, 'r-', x_center, y_center, 'bo');
axis equal
So basically in this code I want to be able to move the slider and the value that I move it to I want it to be set as the variable theta and I want the graph to update with the different thetas as I move the slider.
Also please don't ask me to try another way to rotate a graph, this is the way that I need to do that.
Thanks!

Risposte (1)

John Petersen
John Petersen il 16 Set 2013
you have a callback function already, but you put all your code in the createfcn which doesn't execute after creation. Try putting your code in the slider callback.

Questa domanda è chiusa.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by