I'm using a code to pass values between guis. The first time it worked flawlessly, but the second time it neither gave me the output nor an error. Please help!

This is the code I used. I found this answer submitted by Geoff Hayes in another similar question. The handle visibilty has been changed to on and the tags of my figures have been changed to their respective names. What am i doing wrong?
function pushbutton1_Callback(hObject, eventdata, handles)
% get the handle of Gui1
h = findobj('Tag','Gui1');
% if exists (not empty)
if ~isempty(h)
% get handles and other user-defined data associated to Gui1
g1data = guidata(h);
% maybe you want to set the text in Gui2 with that from Gui1
set(handles.text1,'String',get(g1data.edit1,'String'));
% maybe you want to get some data that was saved to the Gui1 app
x = getappdata(h,'x');
end

6 Comments

Preethi - please clarify what you mean by it neither gave me the output nor an error. What is the workflow of your app? What is not happening that you expect to be happening? From the code above, I'm assuming that it is the pushbutton callback for a button within Gui2. Is that the case?
Geoff,
I have a gui which calculates the length and breadth of an image using the number of pixels. These values are printed in two edit boxes in my first gui(openingpagedemo). I want to pass these values to an edit box in my second gui(page1). This is what is not happening. The following is the code we have used to calculate length and breadth
global b;
desired = [];
for n = 1:size(b,3)
desired = cat(1,desired,b(:,:,n));
end
b = reshape(b,[],size(b,2),1);
[a,c1]=size(b);
count=zeros(a-1);
count1=zeros(c1-1);
for i=1:a
for j=1:c1-1
if (b(i,j)>0) && (b(i,j)==b(i,j+1))
count(i)=count(i)+1;
end
if i<a-1
if(b(i,j)>0)&&(b(i,j)==b(i+1,j));
count1(j)=count1(j)+1;
end
end
end
end
l=max(max(count))+1;
br=max(max(count1))+1;
%angle=asind(l/b)
set(handles.length1, 'String', num2str(l));
set(handles.br1, 'String', num2str(br));
guidata(hObject,handles);
And then in the pushbutton callback of page1,I've used the following code.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = findobj('Tag','openingpagedemo');
% if exists (not empty)
if ~isempty(h)
% get handles and other user-defined data associated to Gui1
g2data = guidata(h);
% maybe you want to set the text in Gui2 with that from Gui1
set(handles.len1,'String',get(g2data.length1,'String'));
% maybe you want to get some data that was saved to the Gui1 app
%x = getappdata(h,'x');
end
Any ideas on what I could do or change?
Preethi - does the above code follow from http://www.mathworks.com/matlabcentral/answers/272736-i-have-two-guis-and-when-i-call-gui2-from-gui1-using-a-continue-button-gui2-does-not-work-however? If so, and page1 is the GUI that is available to the user, does that mean that the openingpagedemo has already been closed? Because I think that was the workflow from your previous question - the user starts with one GUI and at some point presses a button to launch the second GUI with the first GUI closing.
Geoff,
In total I have three pages. openingpagedemo, page1 and fake_final. The other question i had asked was regarding page1 and fake_final. The user starts with the opening page. It follows this order. openingpagedemo->page1->fake_final The connection between page1 and fake_final is working perfectly where page1 closes when fake_final opens and the value transfers successfully. However, I cannot get my width or length value to move from openingpagedemo to page1. I do not get the output in the editbox in page1, nor do I get any error.
Clarifying my question about the error "Attempt to reference field of non-structure array." , this error was seen in a gui called Opening_Page. Since Opening_Page gave many errors, I divided the functions into other guis to sort out one error at a time. One such gui is openingpagedemo(Sorry, I realise I am being rather long winded). Opening_Page still has that error. openingpagedemo does not.
Preethi - when you try to get the values from openingpagedemo, is this GUI still available (i.e. visible to the user) or have you closed it? If it has been closed, then you will not be able to obtain the length and width from it.
Geoff, Yes I was closing it when I moved to page1. I never realised that page1 was never shut which was why it worked. Thank you!!! Now it works perfectly. :)

Sign in to comment.

Answers (0)

Asked:

on 17 Mar 2016

Edited:

on 7 Apr 2016

Community Treasure Hunt

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

Start Hunting!