MATLAB newbie - add element to pop-up menu - matlab

i want to add element to my pop-up menu that user write it in a text field , i'm new to matlab so any explanation is very appreciated , i know it's about a callback in the pushbutton but i don't know how to do it exactly..
My gui so far :
my code so far :
function varargout = popupmenu(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #popupmenu_OpeningFcn, ...
'gui_OutputFcn', #popupmenu_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 popupmenu_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = popupmenu_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function popupmenu1_Callback(hObject, eventdata, handles)
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function pushbutton1_Callback(hObject, eventdata, handles)

In the pushbutton1_Callbackyou might add:
new_item=get(handles.edit_1,'string') % to get the string from the edit box
tmp=get(handles.popupmenu1,'string') % to get the actual list of items in the popup menu
tmp{end+1}=new_item % to add the string
set(handles.popupmenu1,'string',tmp) % to update the popup menu items

i done it , in the callback button :
dropdownListStrings = {'item1',item2'}
handleToListbox = uicontrol('Style', 'popup','Position', [20 60 200 60], 'String', dropdownListStrings);
but how to get the new data from the new popup menu ??
EDIT :
found it
handleToListbox = uicontrol('Style', 'popup','Position', [20 60 200 60], 'String', dropdownListStrings,'Callback',#setmap);
function setmap(source,callbackdata)
val = source.Value
string = source.String(val)
end

Related

Setting handles from one GUI to another GUI - Matlab

I am new to Matlab, excuse my amateur coding. I am trying to pass handles from one GUI to another GUI which are two independent GUI's.
for example, I created two GUI's test1.m and test2.m, in which test2.m calls test1.m in the opening function. so here I am trying to set the text on the test1.m using its handles. But I get an error Reference to non-existent field test1_text. I have even tried sending the handles of test2.m to test1.m by doing test1(handles) in the opening function but still I get the same error.
test2.m sets the text in the second GUI:
function varargout = test2(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #test2_OpeningFcn, ...
'gui_OutputFcn', #test2_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 test2_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
test1
guidata(hObject, handles);
function varargout = test2_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function test2_button_Callback(hObject, eventdata, handles)
str = sprintf('hello');
set(handles.test1_text,'String',str);
test1.m
function varargout = test1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #test1_OpeningFcn, ...
'gui_OutputFcn', #test1_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 test1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = test1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
note that The GUI was developed in Matlab GUIDE.
can anyone advice me on how to do that?
Use GUIDE's Inspector to set Tag to your test1, i.e my_test_1.
In your test2, find the object with such Tag before use it:
function test2_button_Callback(hObject, eventdata, handles)
obj = findall(0, 'Type', 'figure', 'Tag', 'my_test_1');
my_text = findobj(obj, 'Tag', 'test1_text');
str = sprintf('hello');
set(my_text,'String',str);
By the way, you must assure that your test1 has an object named test1_text.

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)

Matlab passing data to another GUI

I am new to Matlab, kindly guide me on this question.
I have two figures, I would like to pass data from first figure to second figure.
Code:
function pushbutton1_Callback(hObject, eventdata, handles)
tryText=get(handles.text2,'String');
open SecondPage.fig
guidata(hObject,handles);
how do I pass the data in "tryText" and use it in second figure?
I tried passing the data using this format:
function pushbutton1_Callback(hObject, eventdata, handles)
tryText=get(handles.text2,'String');
SecondPage(tryText)
guidata(hObject,handles);
but I have no luck retrieving it.
Thanks
Latest Update:
function varargout = first(varargin)
handles=struct;
datas=struct;
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #first_OpeningFcn, ...
'gui_OutputFcn', #first_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 first_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = first_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
struct.a=get(handles.edit1,'String');
set(handles.text1,'String',struct.a);
disp(struct);
setappdata(0,'MyStruct',struct);
open second.fig
Mean while in Second:
function varargout = second(varargin)
getappdata(0,'MyStruct');
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #second_OpeningFcn, ...
'gui_OutputFcn', #second_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 second_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = second_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
disp(struct)
in first figure, the result of disp(struct) is correct, while in second figure, it shows "1x1 struct array with no fields."
Can someone highlight where is my mistake? thanks
You need to use handles and data structures to do that. Instead of showing you how to do it here, I will simply point you to an answer given in MATLAB Central which tells you exactly how you can achieve this.
Thanks for the reply, I finally manage to make it work with the following code.
Code:
function varargout = second(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #second_OpeningFcn, ...
'gui_OutputFcn', #second_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 second_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = second_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
struct=getappdata(0,'MyStruct');
disp(struct)

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.