How to declare static and global variable in MATLAB - matlab

I am using a function of increase and decrease sharpness, contrast of an image but the problem is that I have to get the value of the increased or decreased sharpness or contrast value from the functions to reuse those values. the function of increase and decrease of sharpness or contrast happens when the button is clicked in the GUI. Kindly help me to use and declare static and global variables which will be used in the application.
Main GUI File
function varargout = Cotton_Disease_Detector(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Cotton_Disease_Detector_OpeningFcn, ...
'gui_OutputFcn', #Cotton_Disease_Detector_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
function Cotton_Disease_Detector_OpeningFcn(hObject, eventdata, handles, varargin)
axes(handles.axes1);
axis off
axes(handles.axes4);
axis off
axes(handles.axes5);
axis off
axes(handles.axes6);
axis off
handles.output = hObject;
guidata(hObject, handles);
function varargout = Cotton_Disease_Detector_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function browse_Callback(hObject, eventdata, handles)
image=browseImage();
imshow(image,'Parent',handles.axes1)
function segmentation_image_Callback(hObject, eventdata, handles)
image = imread('enhance.jpg');
image=imageSegmentation(image);
imshow(image,'Parent',handles.axes5);
function enhance_image_Callback(hObject, eventdata, handles)
image=imread('resize.jpg');
set(handles.enhancementPanel,'Visible','On')
image=imageEnhancement(image);
imwrite(image,'enhance.jpg');
imshow(image,'parent',handles.axes4);
function classification_image_Callback(hObject, eventdata, handles)
function feature_extraction_image_Callback(hObject, eventdata, handles)
function decreaseContrast_Callback(hObject, eventdata, handles)
image=imread('enhance.jpg');
[J,x]=decreaseContrast(image,x);
imwrite(J,'enhance.jpg');
imshow(J,'parent',handles.axes4);
function increaseContrast_Callback(hObject, eventdata, handles)
image=imread('enhance.jpg');
[J,x]=increaseContrast(image,x);
imwrite(J,'enhance.jpg');
imshow(J,'parent',handles.axes4);
function decreaseSharpness_Callback(hObject, eventdata, handles)
image=imread('enhance.jpg');
[J,x]=decreaseSharpness(image,x);
imwrite(J,'enhance.jpg');
imshow(J,'parent',handles.axes4);
function increaseSharpness_Callback(hObject, eventdata, handles)
image=imread('enhance.jpg');
[J,x]=increaseSharpness(image,x);
imwrite(J,'enhance.jpg');
imshow(J,'parent',handles.axes4);
function resizeImage_Callback(hObject, eventdata, handles)
image=imread('read.jpg');
image=resizeImage(image);
imwrite(image,'resize.jpg');
Function of increase Sharpness
function [image,x] = increaseSharpness(image,x)
if isempty(x)
x=0.2;
end
x=x+0.2;
image=imsharpen(image,'radius',2,'Amount',x);
end
Function of decrease Sharpness
function [image,x] = decreaseSharpness(image,x)
if isempty(x)
x=0.2;
end
x=x-0.2;
image=imsharpen(image,'radius',2,'Amount',x);
end
The functions of increase decrease Contrast are same as of Sharpness.

Rather than using globals, Matlab provides ways to store data directly on your GUI objects.
This page has a good overview of these methods.
The most appropriate way to share data between calls is to use setappdata(hObject, name, value) to store values on you GUI object and use getappdata(hObject, name) to retrieve them.
Another method is to use the guidata(hObject, data) function allowing you to store a single variable or struct. However, GUIDE GUI's use this function to store a handles object, so the setappdata method is preferred.
[EDIT based on comment from #Hoki]

Related

Matlab - Defining the maximum value of a slider with a pushbutton_callback

I have a GUI. Suppose I want to display a certain number of images. I created a slider to display all the images in one window when you scroll down. The problem occurrs when you scroll down and exceed the number of images, so I need something to control the maximum value of the slider
One solution to this is:
I can create a slider in certain position and with certain max depending on images I selected .
My code right now (does not have the solution I spoke about) :
function varargout = panel(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #panel_OpeningFcn, ...
'gui_OutputFcn', #panel_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
function panel_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = panel_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
global celldata3;
k = 1;
[filename pathname] = uigetfile({'*.*'},'File Selector','MultiSelect', 'on')
iscellstr(filename)
celldata1 = cellstr(pathname)
celldata2 = cellstr(filename)
celldata3 = strcat(celldata1,celldata2)
subplot(3,4,1),imshow(celldata3{1})
subplot(3,4,2),imshow(celldata3{2})
subplot(3,4,3),imshow(celldata3{3})
subplot(3,4,4),imshow(celldata3{4})
subplot(3,4,5),imshow(celldata3{5})
subplot(3,4,6),imshow(celldata3{6})
subplot(3,4,7),imshow(celldata3{7})
subplot(3,4,8),imshow(celldata3{8})
subplot(3,4,9),imshow(celldata3{9})
subplot(3,4,10),imshow(celldata3{10})
subplot(3,4,11),imshow(celldata3{11})
subplot(3,4,12),imshow(celldata3{12})
function slider1_Callback(hObject, eventdata, handles)
global celldata3;
a = get(hObject,'Value')
b = get(hObject,'Max')
[n max] = size(celldata3)
%set(handles.text2,'string',a)
i = (b-a)*4
if i+12 >= max
display ('STOP!');
end
subplot(3,4,1),imshow(celldata3{i+1})
subplot(3,4,2),imshow(celldata3{i+2})
subplot(3,4,3),imshow(celldata3{i+3})
subplot(3,4,4),imshow(celldata3{i+4})
subplot(3,4,5),imshow(celldata3{i+5})
subplot(3,4,6),imshow(celldata3{i+6})
subplot(3,4,7),imshow(celldata3{i+7})
subplot(3,4,8),imshow(celldata3{i+8})
subplot(3,4,9),imshow(celldata3{i+9})
subplot(3,4,10),imshow(celldata3{i+10})
subplot(3,4,11),imshow(celldata3{i+11})
subplot(3,4,12),imshow(celldata3{i+12})
display (i+12)
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function slider1_ButtonDownFcn(hObject, eventdata, handles)

how to send data from a function out GUI matlab to function GUI.m

How can I pass values (x_locate, y_locate) for a static text in my GUI?
Because the function is out functions generated by the GUI. I'm not achieving to configure the set() function.
I believe it is using handles, but I've tried everything and failed.
to simplify I rewrote the code:
![enter image description here][1]
The "Locations.fig" have: 1 axes, 1 pushbutton and 2 static text.
ctrl+C
function varargout = Locations(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Locations_OpeningFcn, ...
'gui_OutputFcn', #Locations_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
function Locations_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Locations_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset');
axes(handles.axes1);
image = imread('eight.tif');
im = imagesc(image);
set(im,'ButtonDownFcn', #clique);
function clique (gcbo,eventdata,handles)
pos = get(gca, 'currentpoint');
x_locate = round(pos(1))
y_locate = round(pos(3)) % until here working!!!
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]); %don´t working
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]); %don´t working
Only callbacks set through GUIDE (or some other custom means) receive the extra handles argument. Otherwise, you'll need to retrieve the handles structure manually within the clique function:
handles = guidata(<object>);
The question of what <object> is depends on your GUI setup. If im is a child of the GUI figure, then gcbo1 will do. If it's in a separate figure, then you'll need to get a handle to the GUI figure. Using findobj to either enumerate all figures or search for some specific property of your GUI figure is the straightforward way to do it.
For example, all Handle Graphics objects have a 'Tag' property that you are free to use, which would be helpful in this case. In GUIDE, set the Tag property on your GUI figure to 'my GUI', then you can retrieve the data from anywhere like so:
hfig = findobj('Tag','my GUI');
handles = guidata(hfig);
[1] Incidentally, giving variables the same name as built-in functions isn't a great idea
Set the ButtonDownFcn to be:
set(im, 'ButtonDownFcn', #(src,evt)clique(src,evt,handles))
The Notlikethat resolved the problem! Thanks!!!
so:
x_locate = round(pos(1));
y_locate = round(pos(3)); % until here working!!!
hfig1 = findobj('Tag','text1');
handles = guidata(hfig1);
hfig2 = findobj('tag','text2');
handles = guidata(hfig2);
set(handles.text1, 'string', ['x loc:' num2str(x_locate)]);
set(handles.text2, 'string', ['y loc:' num2str(y_locate)]);
finalized

Passing outside variables to MATLAB GUIDE

My program loads data from a file and produces a graph, the user clicks on an area of interest and then analysis is done and a new graph is produced. The program continues asking the user to click on the image until the user presses e to exit the program.
I want the graph that is produced to be a GUI that takes data from my program but I seem to have trouble transferring that data into the GUI function. Here is a quick example of what my program looks like:
load(data)
plot(x,y)
while 1%so that it continues asking for user interaction
figure(1)
'click on the point you want or press e to exit'
[x1,y1,key]=ginput(1)
f=score(x1,y1)
%the above is a different function that gives us the data that I want to graph,
%that are called xf,yf
%GUI plot
figure(1)
test_gui(xf,yf)
if (key == 'e')
display('End')
break;
else
display('next point')
end
end
My test_gui.m looks like this:
function varargout = test_gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #test_gui_OpeningFcn, ...
'gui_OutputFcn', #test_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 fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for test_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test_gui_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (xf,yf)
The problem is that when I click on the "Push" button, it does not graph anything so there must be something wrong in the way I pass the xf, yf variables. I was wondering if anyone has any ideas about what I am doing wrong, I have not used GUIDE before and it seems I'm lost.
From the looks of your code, xf and yf are never defined, only f (the result of score). So that could be why you can't see any plots.
Supposing that score dumps xf and yf into the workspace, you must first define them from varargin and then pass them to the callback function using handles, as Werner commented.
% --- Executes just before fft_guide is made visible.
function test_gui_OpeningFcn(hObject, eventdata, handles, varargin)
xf = varargin{0}; yf = varargin{1}; % Get xf and yf from input
handles.xf = xf; handles.yf = yf; % Put the values in handles
guidata(hObject,handles); % Save handles so you can use it anywhere in the GUI
And in the callback:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
plot (handles.xf,handles.yf)
I believe this should work, supposing xf and yf are being correctly define before being passed to the GUI function.

Remnants of MATLAB Popup Selection Example Stay There

I am still learning my way around MATLAB's gui. I used one of GUIDE's templates, precisely the GUI with Axes and Menu.
This generates two files: GUIFigure.m and GUIFigure.fig
At first, I deleted the line that gives labels to the popup menu which goes along the following lines:
function popupmenu1_CreateFcn(hObject, eventdata, handles)
set(hObject, 'String', {'old text 1', 'old text 2');
When I replaced the text inside the curly brackets with my own text, it worked fine. When I replaced it with a cell variable and used incorrect syntax, it would give me an error, which is understandable, but the figure would still pop up with the old text.
Another similar issue is that the example already has a plot with axes. Now I deleted the axes from the .fig file, and I also deleted their code in the .m file. However when I run the gui function, the axes still show up with the plot, although I've even deleted the code that generates the data for the plot!
Any explanation/tips as to what is going on would be appreciated.
function varargout = FIAFigure(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #FIAFigure_OpeningFcn, ...
'gui_OutputFcn', #FIAFigure_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 FIAFigure is made visible.
function FIAFigure_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for FIAFigure
handles.output = hObject;
handles.dataSetsCell = varargin{1};
handles.categoryNames = varargin{2};
% Update handles structure
guidata(hObject, handles);
if strcmp(get(hObject,'Visible'),'off')
plot(rand(5));
end
% --- Outputs from this function are returned to the command line.
function varargout = FIAFigure_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
t = uitable(handles.uitable1);
cla;
popup_sel_index = get(handles.popupmenu1, 'Value');
set(t,'Data',magic(popup_sel_index))
% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
file = uigetfile('*.fig');
if ~isequal(file, 0)
open(file);
end
% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
printdlg(handles.figure1)
% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
['Close ' get(handles.figure1,'Name') '...'],...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(handles.figure1)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
set(hObject, 'String', {handles.categoryNames{1:end}});
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function uitable1_CreateFcn(hObject, eventdata, handles)

Use multiple gui and get data from another gui function in matlab

I want to create a gui program with matlab but i want to use multiple gui. for example I have the main gui function and I want to get data from another gui with edit textbox. In the example below, I want to return the p variable to the main gui.
THE MAIN GUI:
function varargout = FoProgram(varargin)
gui_Singleton = 0;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #FoProgram_OpeningFcn, ...
'gui_OutputFcn', #FoProgram_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
function FoProgram_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = FoProgram_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function labor_2_Callback(hObject, eventdata, handles)
function fel1_Callback(hObject, eventdata, handles)
cla reset;
clc;
clear all;
n = guzu() %Here I call the second Gui function with edit textbox
uiwait(gcf);
x=linspace(-3*pi,3*pi,1000);
y=sin(x);
plot(x,y,'k','LineWidth',4)
sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
hold on
title('Sin(x) Taylor sora')
%n = str2num(N);
f=zeros(size(x));
for i=1:n
t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
f=f+t;
plot(x,f,sz(i),'LineWidth',2)
axis([-10 10 -10 10])
pause(1.5)
hold on
n=n+1;
end
function exit_Callback(hObject, eventdata, handles)
close
THE SECOND GUI
function varargout = guzu(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #guzu_OpeningFcn, ...
'gui_OutputFcn', #guzu_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
function guzu_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = guzu_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
p = str2double(get(hObject,'String')) %I want to return this 'p' to the main gui
close
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
This kind of simple user input is easy to accomplish using the inputdlg function, without the need to create a separate GUI - here's an example
inputTitle = 'Input Required';
inputPrompt = 'Enter a value for ''p'':';
userInput = inputdlg(inputTitle, inputPrompt);
if isempty(userInput)
% User cancelled
return;
else
p = userInput{1}; % userInput is a cell array
% Do something with p
end
It may be that the example you provided is a very minimal version of your end-goal for the second UI, so this may not be appropriate (though note that the inputdlg function is capable of some more complex behaviour - see the documentation). If you wish to continue using your separate UI, then you have to make a couple of modifications
%%% Add this line to the end of OpeningFcn
uiwait;
%%% Modify OutputFcn to have the following code:
varargout{1} = str2double(get(handles.edit1, 'String'));
% The figure can be deleted now
% NOTE: You have to change this to the name of your figure,
% if it's not called figure1
delete(handles.figure1);
%%% Add the CloseRequestFcn callback and put this code in it
if isequal(get(hObject, 'waitstatus'), 'waiting')
% The GUI is still in UIWAIT, us UIRESUME
uiresume(hObject);
else
% The GUI is no longer waiting, just close it
delete(hObject);
end
Most of the above is directly copied from here. You can also remove the call to uiwait from FoProgram.