Index exceed matrix dimensions in GUI
4 次查看(过去 30 天)
显示 更早的评论
Hye i have problem with this error, here is my code in pushbutton1_callback:
global r;
[a, b]=uigetfile('.bmp');
img=imread([b a]);
imshow(img,'Parent',handles.axes1);
if (a =='1.bmp')
d = r(1,:);
elseif (a =='2.bmp')
d = r(5,:);
elseif (a =='3.bmp')
d = r(9,:);
end
and the error is:
--> Index exceeds matrix dimensions.
-->Error in SystemDemo1>pushbutton1_Callback (line 86)
d = r(1,:);
--> Error in gui_mainfcn (line 95)
feval(varargin{:});
-->Error in SystemDemo1 (line 42)
gui_mainfcn(gui_State, varargin{:});
-->Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)SystemDemo1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
and r is actually a variable in matlab workspace. but i want the contents in r which the value is in double to be used in my gui. So, thats why im using global. Hope any of you guys could help me as im new in using function in GUI and it seems complicated/hard for me because im never used it. Any advice or help will be really appreciated. Thank you so much.
5 个评论
Dennis
2018-5-10
There are a quite a few alternatives to global variables. If you want to use globals you need to declare said global in every function/script you are using.
For further information about passing data between callbacks please have a look here: https://de.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
In your case i suggest to either pass r as additional input to your function
function pushbutton1_callback(hObject,~,r)
and when you create your pushbutton:
Callback,{@pushbutton1_callback,r}
or use setappdata/getappdata like Rik Wiselink suggests:
After you created your pushbutton:
setappdata(handles.pushbutton1,'r',r);
In your function:
r=getappdata(hObject,'r');
I do not know if hObject and handles.pushbutton1 are correct in your case since those parts of your code are missing.
回答(1 个)
Ameer Hamza
2018-5-10
编辑:Ameer Hamza
2018-5-10
Have you declared r as global in command window by running
global r
You need to run global r both in the command window and your app code. Another way is to use evalin() to get value from base workspace directly in GUI without using global. Use it like this
r = evalin('base', 'r');
Note that all of these options are unrecommended. See Here: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. You should consider @Rik's suggestion to improve the reliability of your code.
15 个评论
Dennis
2018-5-11
Yes, you can pass other variables or even the entire struct.
setappdata(handles.pushbutton1,'my_data',my_data)
In the code you have shown you are passing p twice and dont pass r
Does your callback function start like this:
function pushbutton1_Callback(hObject,eventdata,handles)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!