Buttondownfcn gui axes - matlab

I have a Gui, that it has an axes...In the axes i can draw lines with plot.. but i want to select a line of the axes.. i have tried with buttondownfcn..but it doesn't work.. i have a button DELETE and its callback is:
hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6,'ButtonDownFcn',('h = copyobj(gcbo,figure)'));
delete_object_axes = findobj(h, 'Type', 'line');
My code is:
% --- Executes on mouse press over axes background.
function axes6_ButtonDownFcn(~, ~, handles)
% hObject handle to axes6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hold all;
set(handles.axes6, 'HitTest', 'off');
set(handles.axes6, 'ButtonDownFcn', {#Delete_Callback, handles}');
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(~, ~, 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)
hold all;enter code here
global h;
set(handles.axes6, 'HitTest', 'off');
h = findobj(handles.axes6, 'Type', 'line');
set(h, 'ButtonDownFcn', {#Delete_Callback});
Help me..how can i select and delete a line??? or move??? there is some manner to do it????
It is very important for me..please!! help me!:)

Below is an example code that should help you achieve what you want, based on this discussion in MATLABcentral. What it does is checl if you clicked on a line, if you did, it modifies it, and if you click it again it is deleted. You can paste it in a file and run.
function init()
close all; clear variables; clc;
ax1=ezplot('sin(x)');
set(ax1,'ButtonDownFcn',#mouseClick);
end
function mouseClick(source, event)
if strcmp(get(source,'Type'),'line')
if get(source,'linewidth')==2
delete(source);
return
end
%// Store the handle "source" someplace
%// Demonstration modification:
set(source,'linewidth',2);
end
end
Edit: here's a bit more complicated example, showing how to save variables to the handles structure (a.k.a. guidata):
function init()
close all; clear variables; clc;
hLine(1) = ezplot('sin(x)'); hold all; hLine(2) = ezplot('cos(x)');
set(hLine(:),'ButtonDownFcn',#mouseClick);
grid on
end
function mouseClick(hObject, eventdata)
handles = guidata(gcf); %#1
try
if handles.delete_object_axes==hObject;
delete(hObject);
handles = rmfield(handles,'delete_object_axes');
else
set(handles.delete_object_axes,'linewidth',1);
set(hObject,'linewidth',2);
handles.delete_object_axes = hObject;
end
catch
if strcmp(get(hObject,'Type'),'line')
handles.delete_object_axes = hObject;
set(hObject,'linewidth',2);
guidata(gca, handles);
else
set(handles.delete_object_axes,'linewidth',1);
end
return;
end
guidata(gca, handles);
end

Related

Passing values from radio buttons into the script in Matlab

So i created GUI in guide that looks like this:
GUI
I want to access data from radio button and then change the variables in my simulation (Bitrate and Modulation are the button groups, Improvement is a single radio button). For example - in the simulation I have a variable Rs=1e9, so when 1Gbps button is selected I want it to remain 1e9, but if 10Gbps button is selected I want it to change its value to 10e9.
Then after hitting Start button I want to start my simulation (which is in different .m file) with given parameters. How can I do it ? (I know about handles idea in matlab, but I don't know how to pass value to the simulation)
That's the code that controls gui - generated by guide. I added some code that starts simulation and close gui window.
function varargout = gui_final(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #gui_final_OpeningFcn, ...
'gui_OutputFcn', #gui_final_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_final is made visible.
function gui_final_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for gui_final
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_final_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 start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation');
% --- Executes on button press in dfe.
function dfe_Callback(hObject, eventdata, handles)
% hObject handle to dfe (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of dfe
% --- Executes on button press in ook.
function ook_Callback(hObject, eventdata, handles)
% hObject handle to ook (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of ook
You can do it using the object "hObject"
Like this: Suppose that you have an slider, everytime the slider is moved the callback is called. You can use it to store a variable. Then, when you press a button you want to use that data for whatever. The code is:
function slider_callback(hObject,eventdata)
sval = hObject.Value;
diffMax = hObject.Max - sval;
data = struct('val',sval,'diffMax',diffMax);
hObject.UserData = data;
% For R2014a and earlier:
% sval = get(hObject,'Value');
% maxval = get(hObject,'Max');
% diffMax = maxval - sval;
% data = struct('val',sval,'diffMax',diffMax);
% set(hObject,'UserData',data);
end
function button_callback(hObject,eventdata)
h = findobj('Tag','slider1');
data = h.UserData;
% For R2014a and earlier:
% data = get(h,'UserData');
display([data.val data.diffMax]);
end
REFERENCE: Matlab Docs
UPDATE
In your case you can do it with another approach if you want. When you press the button START you read the state of your radio buttons, and pass the appropiate value to your simulation_function that I suppose it is called "simulation". In the following example I suppose that you have a button-group called (tag): uibuttongroup1, and inside you have two buttons: radiobutton1 and radiobutton2
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Check which radio button is pressed
switch get(get(handles.uibuttongroup1,'SelectedObject'),'Tag')
case 'radiobutton1', myParameter = 1e9;
case 'radiobutton2', myParameter = 10e9;
end
%Execute simulation
clc
close all
message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation(myParameter)');

Get data cursor callback in Matlab GUI

How can I set the Matlab GUI data cursor callback for a specific GUI axes?
I was able to add the data cursor icon in GUI toolbar. I can selected 3D point on the plotted data, but I need to add more info in the data cursor text and also do some other stuff with the coordinates it is getting.
I tried to follow "How to add additional info to the data cursor?", but doesn't work for GUI.
GUI axes are not figures, so I'm getting this error:
Error using datacursormode (line 149)
Invalid figure handle
I need the same here, but for GUI axes (figures?):
function test_main
% Plots graph and sets up a custom data tip update function
fig = figure('DeleteFcn','doc datacursormode');
X = 0:60;
t = (X)*0.02;
Y = sin(-16*t);
plot(X,Y)
dcm_obj = datacursormode(fig); % tried here "handles.MyFigHandle"
set(dcm_obj,'UpdateFcn',{#myupdatefcn,t})
function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
txt = {['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['I: ',num2str(I)],...
['T: ',num2str(t(I))]};
For now I can get only the default datacursor behavior, like this image:
I've not found any difference between the use of the data cursor in a GUI and in a figure.
Re-using part of your code, I've created a GUI in which a checkbox enable / disable the datacursormode.
The GUI contains two axes in which two lines and a 3D surface are plotted respectively.
Also three radiobuttons control the string that will be printed in the textbox generated when the datacursormode is enabled.
The tags of the GUI are the folowing:
left axes: axes_2d
right axes: axes_3d
left pushbutton: pushbutton_2d
right pushbutton: pushbutton_3d
checkbox: checkbox_enable_dc
left radiobutton: radiobutton1
middle radiobutton: radiobutton2
righ radiobutton: radiobutton3
The GUI works this way:
the pushbuttons plot in the axes and enable the checkbox
the checkbox enable / disable the datacursormode
by default, the left radiobutton is selected and sets the default string
the other two radiobutton set a different string
Based on the selected radiobutton, the first line of the string in the textbox will be either:
DEFAULT STRING
STRING OPTION ONE
STRING OPTION TWO
the string to be written in the textbox is created in your myupdatefcn that has been modified to handle the options selected by the radiobutton.
Notice: in order not to limit the modification to your version of the myupdatefcn function, I've hard coded the t array.
This is the .m file of the GUI:
function varargout = gui_datacursormode(varargin)
% GUI_DATACURSORMODE MATLAB code for gui_datacursormode.fig
% GUI_DATACURSORMODE, by itself, creates a new GUI_DATACURSORMODE or raises the existing
% singleton*.
%
% H = GUI_DATACURSORMODE returns the handle to a new GUI_DATACURSORMODE or the handle to
% the existing singleton*.
%
% GUI_DATACURSORMODE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_DATACURSORMODE.M with the given input arguments.
%
% GUI_DATACURSORMODE('Property','Value',...) creates a new GUI_DATACURSORMODE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before gui_datacursormode_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to gui_datacursormode_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_datacursormode
% Last Modified by GUIDE v2.5 02-Apr-2017 17:45:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #gui_datacursormode_OpeningFcn, ...
'gui_OutputFcn', #gui_datacursormode_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_datacursormode is made visible.
function gui_datacursormode_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_datacursormode (see VARARGIN)
% Choose default command line output for gui_datacursormode
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui_datacursormode wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_datacursormode_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 pushbutton_2d.
function pushbutton_2d_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_2d (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Plot two lines in the first axes
t=0:.1:2*pi;
plot(handles.axes_2d,t,sin(t),'r');
hold(handles.axes_2d,'on')
plot(handles.axes_2d,t,cos(t),'b');
% Enable the checkbox that will set datacorsormode on
handles.checkbox_enable_dc.Enable='on';
% --- Executes on button press in pushbutton_3d.
function pushbutton_3d_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_3d (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Plot a 3D graph in the second axes
axes(handles.axes_3d);
peaks
% Enable the checkbox that will set datacorsormode on
handles.checkbox_enable_dc.Enable='on';
function txt = myupdatefcn(~,event_obj,t)
% Customizes text of data tips
% Get the handles of the GUI to access to the radiobuttons
my_guidata=guidata(gcf);
% Define the additional string to be written
if(my_guidata.radiobutton1.Value)
str='DEFAULT STRING ';
elseif(my_guidata.radiobutton2.Value)
str='STRING OPTION ONE ';
else
str='STRING OPTION TWO ';
end
% Get the datacursor data
pos = get(event_obj,'Position');
I = get(event_obj, 'DataIndex');
% Create the whole string to be written
txt = {[str], ...
['X: ',num2str(pos(1))],...
['Y: ',num2str(pos(2))],...
['I: ',num2str(I)],...
['T: ',num2str(t(I))]}
% --- Executes on button press in checkbox_enable_dc.
function checkbox_enable_dc_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_enable_dc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% If the checkbox is set
if(hObject.Value)
% enable the radiobuttons that control the string to be written
handles.radiobutton1.Enable='on';
handles.radiobutton1.Value=1;
handles.radiobutton2.Enable='on';
handles.radiobutton3.Enable='on';
% Create the datacursormode object
dcm_obj = datacursormode(gcf)
t=rand(1,10000);
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on', ...
'UpdateFcn',{#myupdatefcn,t})
else
% If the checkbox is not set, disable the datacursormode
datacursormode 'off'
end
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% Toggle the other radiobuttons
handles.radiobutton2.Value=0
handles.radiobutton3.Value=0
% --- Executes on button press in radiobutton1.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% Toggle the other radiobuttons
handles.radiobutton1.Value=0
handles.radiobutton3.Value=0
% --- Executes on button press in radiobutton1.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% Toggle the other radiobuttons
handles.radiobutton1.Value=0
handles.radiobutton2.Value=0
Hope this helps,
Qapla'

Rotating an entire axes in Matlab Guide

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'

Make the slider as a progress bar in matlab GUIDE

I am having hard time to make slider to behave as a progress bar.
I have two push buttons. One accepts the starting value from a text editor(User Input) and other start the progress of the loop.
Then I have got a slider, which I am trying to move as the function loop changes 'i' value(Update along/show progress).
This is my code.
function varargout = myfig(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #myfig_OpeningFcn, ...
'gui_OutputFcn', #myfig_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 myfig is made visible.
function myfig_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 myfig (see VARARGIN)
% Choose default command line output for myfig
handles.output = hObject;
handles.min = get(handles.ed,'value');
handles.max = 10000;
handles.i = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myfig wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myfig_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 pb.
function pb_Callback(hObject, eventdata, handles)
% hObject handle to pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
value = str2double(get(handles.ed,'string'));
value
assignin('base','value',value)
% --- Executes on slider movement.
function sl_Callback(hObject, eventdata, handles)
% hObject handle to sl (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
set(handles.sl,'Max',handles.max);
set(handles.sl,'min',handles.min);
set(handles.sl,'value',handles.i);
% --- Executes during object creation, after setting all properties.
function sl_CreateFcn(hObject, eventdata, handles)
% hObject handle to sl (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
function ed_Callback(hObject, eventdata, handles)
% hObject handle to ed (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,'String') returns contents of ed as text
% str2double(get(hObject,'String')) returns contents of ed as a double
% --- Executes during object creation, after setting all properties.
function ed_CreateFcn(hObject, eventdata, handles)
% hObject handle to ed (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pbs.
function pbs_Callback(hObject, eventdata, handles)
% hObject handle to pbs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myfile;
function [] = myfile(handles)
%MYFILE Summary of this function goes here
% Detailed explanation goes here
% prompt('Please enter a valid number to start');
h = waitbar(0,'Processing...');
% i = input('Please enter a valid number to start');
i = evalin('base','value');
while(i < 10000)
clc
i = i + 1;
waitbar(i/10000,h)
handles.i = i;
set(handles.sl,'value',i/10000);
guidata(hObject,handles);
end
close(h);
Matlab verstion: 2014b
First: I want to learn how can I handle this when function is nested inside the GUI file.
Second: If you move the function into a different .m file.[Using set/get/guidata etc... and not passing input/output with function call]
Please let me know.
For the first case, you will need to modify your function myfile. Before the while loop, you'll need to set the min and max of the slider. This will allow you to use i as your value.
set(handles.sl,'Min',1,'Max',10000);
To allow the slider to update, you will need to add refresh and drawnow inside your loop. Matlab will wait to draw ui changes until the end of your code if you so not use these. Your loop should look like the following:
while(i < 10000)
clc
i = i + 1;
waitbar(i/10000,h)
set(handles.sl,'value',i/10000);
refresh;
drawnow;
end
From personal experience, the second case can get messy for larger applications. I would recommend creating your application using object oriented programming if you need to use more than one file. You could store ui handles in the objects you create, which keeps your code organized and makes it easier to pass handles back and forth.

Viewing image displayed on axes

(http://s1273.photobucket.com/user/Chethan_tv/media/CBIR_zpsb48bce14.jpg.html)
Above image is my final output, open push button is used to view image displayed on respective axes.
I've used following code for displaying
function open1_Callback(filename, hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)
Where, 6 is axes number. But I'm getting error like
undefined variable handles
To display image on axes I've used following code
function displayResults(filename,hObject, eventdata, handles)
% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
guidata(hObject,handles)
filename is a text file.
HOW TO DISPLAY RESPECTIVE IMAGE USING PUSHBUTTON?
This is because you messed the code generated by GUIDE.
Normally callback functione definition looks like this:
function SomeButton_Callback(hObject, eventdata, handles)
But in your code you write
function open1_Callback(filename, hObject, eventdata, handles)
But guide still sends three arguments to the callback function (hObject, eventdata, and handles) in this specific order. So MatLab gets confused and throws an error.
You better put your filename into handles structure in *_OpeningFcn function and then use it from there in all calbacks.
At the end of your *_OpeningFcn you should add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function of your button
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
And yes, all the files you open with fopen function should be closed with fclose. You will learn it hard way when you'll not be able to update your file in your favorite editor because another programs is using it.
See also (Making universal variables in MATLAB GUI)
UPDATE to reflect discussion in comments:
To achieve the behavior you want I'd do the following:
At the end of your *_OpeningFcn add the following:
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
imagename = fgetl(fid);
if ~ischar(imagename), break, end % Meaning: End of File...
[x,map]=imread(imagename);
rgb=ind2rgb(x,map);
ax = handles.(sprintf('axes%d', N));
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
%xlabel(imagename);
% store file name and image itself for later use
handles.MyData.Images{N} = rgb;
handles.MyData.FileNames{N} = imagename;
% we have buttons like open1 open2 open3 etc...
% add some info to the open buttons
% so they will be aware which image they display
btn = handles.(sprintf('open%d', N));
set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE
% Update handles structure
guidata(hObject, handles);
Then in callback function for your open button do the following
function open1_Callback(hObject, eventdata, handles)
% hObject handle to open1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
N = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
Basically, this callback is universal: it should work without modifications for all your open buttons. You may even change callback function of open2, open3 ... buttons in GUIDE to open1_Callback.