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

2 visualizzazioni (ultimi 30 giorni)
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.

Risposte (1)

Walter Roberson
Walter Roberson il 28 Lug 2021
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.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by