error = Index exceeds matrix dimension
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
Walter Roberson
2011년 3월 10일
We need the code for connect_m2_Callback
fremond khoo
2011년 3월 10일
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)
채택된 답변
Walter Roberson
2011년 3월 10일
0 개 추천
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
fremond khoo
2011년 3월 11일
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)?
Walter Roberson
2011년 3월 11일
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.
fremond khoo
2011년 3월 11일
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?
Walter Roberson
2011년 3월 11일
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.
fremond khoo
2011년 3월 11일
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?
fremond khoo
2011년 3월 13일
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
Walter Roberson
2011년 3월 13일
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
fremond khoo
2011년 3월 14일
thanks a lot walter^^..you are the best!!
fremond khoo
2011년 3월 14일
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
Walter Roberson
2011년 3월 14일
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
fremond khoo
2011년 3월 14일
ok..thanks a lot walter^^
fremond khoo
2011년 3월 15일
i had successfully run both of my motor^^..thanks a lot walter^^
fremond khoo
2011년 3월 15일
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?..
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
참고 항목
2011년 3월 10일
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
