Could someone explain the error: "Array indices must be positive integers or logical values," and also how I'd go about fixing it?

2 次查看(过去 30 天)
I'm aware people have asked similar questions, but I feel like this error can be very vague in terms of what needs to be fixed.
I'm not very experienced with matlab, so it is likely a syntax error or something on that level of simplicity (I hope)
Here's the error:
Error in Interpolation_GUI_SU21>EngSi_Callback (line 479)
UnitType = contents(get(hObject, 'Value'));
Error in Interpolation_GUI_SU21>editT_Callback (line 82)
EngSi_Callback(hObject, eventdata, handles)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Interpolation_GUI_SU21 (line 42)
gui_mainfcn(gui_State, varargin{:});
I'm making a GUI, and my .m file is over 600 lines in total, but luckily (I believe) I've localized the problem to somewhere in here:
Further information: A17 & A17E (below) are two separate 25x3 matrices. Values are pulled from one or the other depending on what units the user selects in the pop up menu, but whichever matrix is selected is stored under handles.A.
contents = cellstr(get(hObject, 'string'));
UnitType = contents(get(hObject, 'Value'));
if (strcmp(UnitType, 'International (Si)'))
Value = A17;
handles.A = A17;
elseif (strcmp(UnitType, 'Imperial'))
Value = A17E;
handles.A = A17E;
end
If anyone suspects my error is located elsewhere, and more detail/more context/more of the code is needed, I'd be happy to provide it; I just didn't want to make things overly-cluttered and complicated.

回答(1 个)

Walter Roberson
Walter Roberson 2021-7-28
contents = cellstr(get(hObject, 'string'));
UnitType = contents(get(hObject, 'Value'));
In the situation where nothing is selected in hObject, and hObject refers to a uicontrol listbox or drop box, then the Value property can be empty. Indexing a cell array at empty index is not an error. (If I recall correctly, there is also one initialization method that can result in Value being 0 for a listbox or dropbox that used to have an entry selected, but had its String property changed to alter the number of possible entries to fewer than the the index of what was selected before.)
However, when hObject refers to a pushbutton or radio button, then when the object is not selected, the Value property will be equal to the Min property of the uicontrol, with the default Min property being 0. Using 0 as an index is an error.
You should be testing for the case where nothing has been selected.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by