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.
Related
I am trying to send data from my arduino to matlab and use the GUI. I want to read continously the data even when no button is pressed. In order to so, i have to use the fscanf function but i don't know where to put it. There is definitely a while loop that waits the events(such as a pushed button) in which this function should be placed. I am just a beginner so this might be a silly question for you.
Thank you in advance!
function varargout = UltraPlot(varargin)
global a;
global k;
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #UltraPlot_OpeningFcn, ...
'gui_OutputFcn', #UltraPlot_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
disp('Ultraplot');
function UltraPlot_OpeningFcn(hObject, eventdata, handles, varargin)
global a;
global k;
a = serial('COM3');
fopen(a);
handles.output = hObject;
guidata(hObject, handles);
disp('Opening');
function varargout = UltraPlot_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
global a;
global k;
disp('varargout');
function Start_Callback(hObject, eventdata, handles)
global a;
global k;
fprintf(a,'%d',1);
disp('Button pressed');
You have to set Matlab to wait for any data from Arduino in a while loop, check this sample code :
clear;clc;
S=serial('com18'); % Create an S Object
data=0;
set(S,'inputbuffersize',4096,'timeout',20); % Set serial communication parameter
fopen(S); % Open serial communcation
while (1)
if s.bytesavailable>0 % If data from Arduino is available
data=fscanf(S);
data = str2num(data);
% Do whatever you want with data here...
end
data=0;
end
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)
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
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)
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.