varargout isn't working for output from GUI when same formatting works just fine in similar GUI

I'm trying to get output from a GUIDE GUI by passing back to the calling data analysis script and when I press the finish button in my gui it completes uiresume(handles.object) which was previously set by uiwait(handles.object).
Then it gets to the following section.
function varargout = GUI_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)
disp(nargout)
if handles.done==1
handles.output={handles.value1,handles.value2};
% Get default command line output from handles structure
varargout = handles.output;
delete(hObject)
end
It then gives me the error:
Too many output arguments.
despite calling with this
[output1,output2]=GUI(input1(:,m),input2(:,m));
output1 and output2 are both non-array doubles and the
The above functions, in the end, also don't close the GUI despite calling for delete(hObject) and when I use the actual exit window button it causes the error
Error while evaluating CloseRequestFcn
and it tells me
??? Error using ==> plot
Error in color/linetype argument
as part of the same CloseRequestFcn error when I never even use color/linetype arguments in this GUI.
Then because of the CloseRequestFcn error I'm never able to reinitialize the same GUI until after I close out of the Matlab session which if I click the exit button cause Matlab to enter an infinite loop preventing it from closing and forcing me to use Ctr+Alt+Delete to exit.
What is most infuriating about this is that I have used the same format for opening and closing a similar GUI from a similar script that fully functions. Actually the previous GUI was copied and pasted and altered to fit the requirements. The process of me passing variables back is exact same to, the only difference is that I was passing two arrays in the original GUI instead of two non-array doubles. However the "Done" button and its callback function have not changed nor did the OutputFcn
The "Done" button callback is as follows
% --- Executes on button press in complete.
function complete_Callback(hObject, eventdata, handles)
% hObject handle to complete (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.done=1;
handles=guidata(hObject);
guidata(hObject,handles);
uiresume(handles.object);
What is happening? How do I fix it?
I thank anyone and everyone who can help in advance.

Answers (2)

Unless I am missing something you only assign 1 output argument (one cell array of two cells):
handles.output={handles.value1,handles.value2};
so calling it asking for two output arguments would give you that message.
Off the top of my head I can't actually think how you assign more than one output argument. I rarely use output arguments from a GUI, but when I do I think I just output one variable that contains all the outputs I want - e.g. a struct or an array or whatever is appropriate.
Do you need two distinct outputs or can you just have the one output and then interrogate the cells of that output afterwards to divide it up if required?

6 Comments

That's the thing, when I say I have done the same thing in a previous GUI, I stored the two things variables which were arrays in that case and then passed them to the main function via
varargout=handles.output;
I have also tried separating them and passing them that way (shown below) , and it doesn't work. It results in the same error.
varargout(1)=handles.output(1);
varargout(2)=handles.output(2);
To an extent, my biggest annoyance is the resulting requirement to have to force quit all of Matlab.
I would suggest just using:
output=GUI(input1(:,m),input2(:,m));
output1 = output{1};
output2 = output{2};
to get your two outputs which should work with the code you originally posted in the question, though only if they are sufficiently different outputs that you prefer to deal with them in their own variable. If they are just two similar results in a cell array I would probably just keep referring to them as
output{1}
(though I would use a more descriptive name than 'output' for the variable too!)
Someone else might know the correct syntax for passing multiple output arguments though.
I tend to like wrapping up input or output arguments in a struct (or class in my case) just to keep the inputs and outputs tidier.
I will give this a try. I haven't tried it yet, but I will also check to see if using deal() will work. I have never been able fully remember which commands require which data structure types. I don't think deal() will work, but I will give it a try.
As and addition to the original post, the section that produces
??? Error using ==> plot
Error in color/linetype argument
may be occurring because the GUI might be trying to re-initialize without ever closing, but after it has already cleared its workspace and doesn't have anything to fill it's variables because I also get an error that says that
handles.torque=varargin{1};
exceeds dimensions or something along those lines.(varargin at this point doesn't contain anything) This only occurs after the initial error out of the GUI too, so it's not because I've incorrectly passed the data to the GUI.
I don't know for certain that this is what is happening, but any insight might help me. This information may also help get to the root of the problem.
As for what the outputs are,
output1=handles.torque_value; output2=handles.torque_value_index;
This still boggles my mind that I ever got the original way to work in my first GUI. I quite frankly don't remember how I managed to even do so.
@Adam: on occasion I use varargout for functions (not for GUI callbacks), and that is how varargout> works: each cell corresponds to one output argument. So a cell array with two cells does allow two output arguments to be used.
I did something to see if it would prevent the GUI from trying to to reopen.
I put this ---
if getappdata(0,'initiationvalue') == 1
delete(hObject)
end
in the opening function, and this ---
handles.done=1;
setappdata(0,'initiationvalue',1)
guidata(hObject,handles);
uiresume(handles.HIB);
in the callback for the "Done" button.
This prevents the following error from showing up.
??? Error using ==> plot
Error in color/linetype argument
and the error from
handles.torque=varargin{1};
which was
Index exceeds matrix dimensions.
The original problem shouldn't be a problem in the calling script even if it was a loop because it would still have to pass all the same variables back to the GUI because they aren't deleted in the GUI or the script. Even so, my code shouldn't cause the GUI to appear more than once for the same set of data. The GUI by the way is for hand selection of data that was missed by the automated data analysis performed by the script. So I don't know my GUI tries reinitialize, but I figured out how to bypass it.
Using deal() worked. And I figured out why, my program kept trying to re-loop.
I still don't know why not using deal() worked in my other GUI though, which really is the original problem. What is different about the the way I tried passing variables in this GUI from the way I did in the original when as far as I know they aren't?

Sign in to comment.

Perhaps the problem is really your if operator means that there are some instances where the callback is called but the if does not run and so varargout has not values to return (it defaults to empty). Try putting an else case to define varagout for those cases:
if handles.done==1
....
else
varargout = cell(1,nargout);
end

Asked:

on 19 Nov 2015

Commented:

on 20 Nov 2015

Community Treasure Hunt

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

Start Hunting!