Getting an error message of "???Index exceeds matrix dimensions" in GUI when trying to access varargin{1}

2 views (last 30 days)
When I try to take in a value from varargin to my handles structure I get an error message:
??? Index exceeds matrix dimensions.
Error in ==> editTask>editTask_OpeningFcn at 69
handles.mytask = varargin{1};
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> editTask at 42
gui_mainfcn(gui_State, varargin{:});
Here's the section of my code where the error occurs:
function varargout = editTask(varargin)
% EDITTASK M-file for editTask.fig
% EDITTASK, by itself, creates a new EDITTASK or raises the existing
% singleton*.
%
% H = EDITTASK returns the handle to a new EDITTASK or the handle to
% the existing singleton*.
%
% EDITTASK('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EDITTASK.M with the given input arguments.
%
% EDITTASK('Property','Value',...) creates a new EDITTASK or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before editTask_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to editTask_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help editTask
% Last Modified by GUIDE v2.5 04-Mar-2014 15:01:25
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @editTask_OpeningFcn, ...
'gui_OutputFcn', @editTask_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 editTask is made visible.
function editTask_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to editTask (see VARARGIN)
% Choose default command line output for editTask
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes editTask wait for user response (see UIRESUME)
% uiwait(handles.figure1);
handles.mytask = varargin{1};
%Sets initial values for title
ht = findobj('Tag','edit_title');
set(ht, 'String', mytask.title);
% %Sets initial values for optimistic case
hom = findobj('Tag', 'edit_o_m');
om_init = floor(mytask.duration(1)/240);
set(hom, 'String', num2str(om_init));
hod = findobj('Tag', 'edit_o_d');
od_init = floor((mytask.duration(1) - om_init*240)/8);
set(hod, 'String', num2str(od_init));
hoh = findobj('Tag', 'edit_o_h');
oh_init = floor(mytask.duration(1) -od_init*8 - om_init*240);
set(hoh, 'String', num2str(oh_init));
% %sets initial values for most likely case
hmm = findobj('Tag', 'edit_m_m');
mm_init = floor(mytask.duration(2)/240);
set(hmm, 'String', num2str(mm_init));
hmd = findobj('Tag', 'edit_m_d');
md_init = floor((mytask.duration(2) - mm_init*240)/8);
set(hmd, 'String', num2str(md_init));
hmh = findobj('Tag', 'edit_m_h');
mh_init = floor(mytask.duration(2) -md_init*8 - mm_init*240);
set(hmh, 'String', num2str(mh_init));
% %Sets initial values for pessimistic case
hpm = findobj('Tag', 'edit_p_m');
pm_init = floor(mytask.duration(3)/240);
set(hpm, 'String', num2str(pm_init));
hpd = findobj('Tag', 'edit_p_d');
pd_init = floor((mytask.duration(3) - pm_init*240)/8);
set(hpd, 'String', num2str(pd_init));
hph = findobj('Tag', 'edit_p_h');
ph_init = floor(mytask.duration(3) -pd_init*8 - pm_init*240);
set(hph, 'String', num2str(ph_init));
%
% %Sets initial value for cost
hc = findobj('Tag', 'edit_cost');
cost_init = mytask.cost;
set(hc, 'String', num2str(cost_init));
I've looked up the definition on varargin but it didn't help. Does anyone have any advice?

Accepted Answer

Image Analyst
Image Analyst on 8 Mar 2014
You need to pass in something. You are probably just clicking the green triangle so nargin = 0 and varargin is null. then you're trying to access the first element of varargin:
handles.mytask = varargin{1};
which will fail if vargargin is empty. Robust code should check if nargin is 0 or varagin is empty (or both) and do something to handle that situation. Either fix it somehow or bail out.
if nargin == 0 || isempty(varargin)
errorMessage = sprintf('Error: you did not enter any input(s)!');
uiwait(warndlg(errorMessage));
end
  2 Comments
Stephen
Stephen on 8 Mar 2014
When I add that error check and I pass in an object by typing "editTask(task)" in the command window I get this different error message:
??? Undefined variable "mytask" or class "mytask.title".
Error in ==> editTask>editTask_OpeningFcn at 74
set(ht, 'String', mytask.title);
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> editTask at 42
gui_mainfcn(gui_State, varargin{:});
Image Analyst
Image Analyst on 9 Mar 2014
You have to look at what the "tag" property of your main figure is. Is it "mytask"? If it is and you want to set the titlebar, you have to use the 'name' property.
For example
% Give a name to the title bar.
set(gcf, 'Name', 'Awesome Demo by ImageAnalyst', 'NumberTitle', 'Off')
% You can use handles.mytask instead of gcf if you want.
% Get the name back from the title bar.
mytask.title = get(gcf, 'Name');
% Load the name into the "ht" control (whatever that is).
set(ht, 'String', mytask.title);

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!