Index exceed matrix dimensions in GUI

4 ビュー (過去 30 日間)
Nurul Atifah
Nurul Atifah 2018 年 5 月 10 日
コメント済み: Dennis 2018 年 5 月 11 日
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
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.
Nurul Atifah
Nurul Atifah 2018 年 5 月 10 日
編集済み: Nurul Atifah 2018 年 5 月 10 日
my function is
function pushbutton1_Callback(hObject, eventdata , handles)
as this the one that comes out when i want to edit my callback function.

サインインしてコメントする。

回答 (1 件)

Ameer Hamza
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 件のコメント
Nurul Atifah
Nurul Atifah 2018 年 5 月 11 日
編集済み: Nurul Atifah 2018 年 5 月 11 日
ohh i see. so the variables is in the function workspace. is it possible if i can view the function workspace cause it makes it more easy to know whether im doing it wrong or not.
and if i want not only r , so did i just add the variables in the code that you show to me? eg:
if isfield(my_data,'r')==1 %checks if r exists in my_data
r=my_data.r; %gets r from structure
setappdata(handles.pushbutton1,'p',p)
elseif isfield (my_data,'p') ==1
p = my_data.p
setappdata(handles.pushbutton1,'p',p)
end
and i still got this error where my pic wont display
-->Undefined variable "handles" or class "handles.axes1".
-->Error in SystemDemo1>pushbutton1_Callback (line 92)
imshow(img,'Parent',handles.axes1);
-->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
Dennis
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 ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by