error = Index exceeds matrix dimension

good day to all matlab users and experts here,
i face some problems at work and hope to have some guidance from you all. i am working on my robotic arm project and i have develop a GUI programme to connect and run two stepper motor (motor 1 and motor 2)
i have succeeded in running motor 1 but motor 2 is not working. When i click the button CONNECT, this error came out:
??? Error using ==> serial.subsref at 137 Index exceeds matrix dimensions.
Error in ==> GUI_xyplotter>connect_m2_Callback at 129 obj2 = obj2(2)
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> GUI_xyplotter at 16 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)GUI_xyplotter('connect_m2_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
can i know what is the problem and how to solve it?..thanks a lot in advance:)

2 Commenti

We need the code for connect_m2_Callback
the code for connect_m2_Callback:
% --- Executes on button press in connect_m2.
function connect_m2_Callback(hObject, eventdata, handles)
obj2 = instrfind('Type','serial','Port','COM9','Tag','');
if isempty(obj2)
obj2 = serial('COM9');
else
fclose(obj2);
obj2 = obj2(2)
end
fopen(obj2);
(my COM port is COM9)

Accedi per commentare.

 Risposta accettata

Walter Roberson
Walter Roberson il 10 Mar 2011

0 voti

instrfind used with a property-value pair returns an array of objects with the desired properties. As one of the properties is the port, in order for the array to have more than one element, there would have to be more than one serial object with that exact port. That seems an unlikely situation, so I do not see why your code assumes that there will be at least two objects returned when you go ahead and access obj2(2) ?

13 Commenti

erm..it means?..because my MATLAB GUI had to be designed to connect and run two stepper motor..first i use obj1(1) to run the first motor..the second one i use obj2(2)..is my code wrong?..should it be obj1(2)?
But a unique COM port is used for each stepper motor, right? Only one object will match the find for COM9, so obj2 will be at most length 1; when you created obj1 that was for COM8, right? Your instrfind() is not looking for *all* serial ports, only for COM9. So when obj2 is non-empty it will be the single COM9 serial object directly.
yeah..correct..my obj1 is only for COM8 and obj2 for COM9..can i ask how to program the instrfind to look for all serial ports?..and how do i solve the problem?
objs = instrfind('Type','serial');
You can then get(objs(1),'Port') to determine the COM port associated with the object.
You have not really indicated what you want to do with the objects. There is probably no need to close() the serial port object that you are not using -- just close them both before you exit the program. The only thing you might need to do is use something like
handles.activeport = objs(K);
where objs(K) is the open one that matches the serial port you want to talk to.
erm..i dun quite understand the function of this..so it means i need to write like this?
% --- Executes on button press in connect_m1.
function connect_m1_Callback(hObject, eventdata, handles)
%Create a serial port object
objs = instrfind('Type','serial');
if isempty(objs)
objs = serial('Port');
get(objs(1),'Port')
end
fopen(objs);
where should i write the handles.activeport=objs(K) at?
im sorry to trouble you walter..but can i ask help from you?..how do i change the program like what u teach?..is my codes which i wrote above correct?..if not then how should i write them?..really need ur help^^..thanks a lot
Use your desired baud rates instead of 115200
% --- Executes on button press in connect_m2.
function connect_m2_Callback(hObject, eventdata, handles)
obj2 = find_and_open('COM9', 115200);
function connect_m1_Callback(hObject, eventdata, handles)
obj1 = find_and_open('COM8', 115200);
function obj = find_and_open(port, wantbaud)
obj = instrfind('Type','serial','Port',port);
if isempty(obj); obj = serial(port); end
if get(obj, 'BaudRate') ~= wantbaud
if strcmpi(get(obj, 'Status'),'open'); fclose(obj);end
set(obj, 'BaudRate', wantbaud);
end
if ~strcmpi(get(obj, 'Status'),'open'); fopen(obj); end
thanks a lot walter^^..you are the best!!
now i face another problem..first motor has no problem but the second one still got problem..when i choose COM9 from the popupmenu, this problem came:
??? Error while evaluating uicontrol Callback
??? Undefined function or variable 'obj2'.
Error in ==> GUI_xyplotter>popupmenu_com_m2_Callback at 98
a = fprintf(obj2,'%s\n','U1');
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> GUI_xyplotter at 16
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)GUI_xyplotter('popupmenu_com_m2_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
did i type something wrong?..my coding for motor two is like this:
% --- Executes on selection change in popupmenu_br_m2.
function popupmenu_br_m2_Callback(hObject, eventdata, handles)
a = fprintf(obj2,'%s\n','U1');
b = fprintf(obj2,'%s\n','U2');
c = fprintf(obj2,'%s\n','U4');
d = fprintf(obj2,'%s\n','U6');
switch get(handles.popupmenu_br_m2,'Value')
case 1
get(a);
case 2
get(b);
case 3
get(c);
case 4
get(d);
end
% --- Executes during object creation, after setting all properties.
function popupmenu_com_m2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu_br_m2.
function popupmenu_br_m2_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function popupmenu_br_m2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in connect_m2.
function connect_m2_Callback(hObject, eventdata, handles)
obj2 = find_and_open('COM9',9600);
function obj=find_and_open(port,wantbaud)
obj = instrfind('Type','serial','Port',port);
if isempty(obj);
obj = serial(port);
end
if get(obj,'BaudRate')~=wantbaud
if strcmpi(get(obj,'Status'),'open');
fclose(obj);
end
set(obj,'BaudRate',wantbaud);
end
if ~strcmpi(get(obj,'Status'),'open');
fopen(obj);
end
You create obj2 and then you throw the value away.
Please read
http://matlab.wikia.com/index.php?title=FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
ok..thanks a lot walter^^
i had successfully run both of my motor^^..thanks a lot walter^^
good day walter,
can i ask now that i have successfully run both of my motor using MATLAB GUI, is it possible for me to program my two-link robotic arm to draw a straight line?..

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by