Error using patch Vectors must be the same lengths.

조회 수: 7 (최근 30일)
Varun Kumar
Varun Kumar 2019년 5월 31일
편집: Varun Kumar 2019년 6월 3일
function popupmenu7_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu7 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu7
v=get(handles.popupmenu7,'Value')
if v == 1
X1=get(handles.edit1, 'String');
X2=get(handles.edit4, 'String');
X3=get(handles.edit7, 'String');
X4=get(handles.edit10, 'String');
Z=[X1 X2 X3 X4]
elseif v == 2
Y1=get(handles.edit2, 'String');
Y2=get(handles.edit5, 'String');
Y3=get(handles.edit8, 'String');
Y4=get(handles.edit11, 'String');
Z=[Y1 Y2 Y3 Y4]
elseif v == 3
Z1=get(handles.edit3, 'String');
Z2=get(handles.edit6, 'String');
Z3=get(handles.edit9, 'String');
Z4=get(handles.edit12, 'String');
Z=[Z1 Z2 Z3 Z4]
end
image=uigetfile('*.png', 'File Selector');
imshow(image)
axes=handles.axes1;
hold on;
maxAllowablePoints = 5; % Whatever you want.
numPointsClicked = 0;
promptMessage = sprintf('Left click up to %d points.\nRight click when done.', maxAllowablePoints);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
while numPointsClicked < maxAllowablePoints
numPointsClicked = numPointsClicked + 1;
[x(numPointsClicked), y(numPointsClicked), button] = ginput(1)
if button == 3
% Exit loop if
break;
end
end
% Print to command window
x
y
Z
msgbox('Done collecting points');
patch(handles.axes1, x,y,Z, 'Facecolor', 'interp')
hold off
i am using this code to plot on the image but getting error as follows
Error using patch
Vectors must be the same lengths.
Error in imgplt3d>popupmenu7_Callback (line 643)
patch(handles.axes1, x,y,Z, 'Facecolor', 'interp')
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in imgplt3d (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)imgplt3d('popupmenu7_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
please help me
  댓글 수: 2
KSSV
KSSV 2019년 5월 31일
The error clearly says (x,y,Z) are of not same size. Check the dimensions of each.
Varun Kumar
Varun Kumar 2019년 5월 31일
but (x,y,Z) are of same dimension (1row, 4columns)

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 6월 3일
편집: Stephen23 2019년 6월 3일
The problem is very simple: you never actually convert the string data to numeric.
Replace all of your Z definitions with this:
Z = str2double({X1,X2,X3,X4});
You will also need to pay attention that x and y have the same number of elements as Z, or otherwise have permitted sizes according to the patch documentation (your code does not do this).
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2019년 6월 3일
varun - your verson of MATLAB may require you to use a property name to indicate the axes. Try doing
patch(x,y,Z, 'Facecolor', 'interp', 'Parent', handles.axes1)
Varun Kumar
Varun Kumar 2019년 6월 3일
편집: Varun Kumar 2019년 6월 3일
thank you so much it worked but one thing complete patch is having same color. i am using the 2013b version is that the reason

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2019년 5월 31일
Varun - your Z seems to be 1x4 array, but the x and y are 1x5. Your code
maxAllowablePoints = 5;
numPointsClicked = 0;
while numPointsClicked < maxAllowablePoints
numPointsClicked = numPointsClicked + 1;
[x(numPointsClicked), y(numPointsClicked), button] = ginput(1)
if button == 3
% Exit loop if
break;
end
end
Assuming that the button is never 3 (which I suppose it can) and since your numPointsClicked starts at zero, then your x and y can be (at most) of dmension 1x5. Just change maxAllowablePoints to 4 and you should be fine.
  댓글 수: 1
Varun Kumar
Varun Kumar 2019년 6월 3일
still same error is appearing eventhough i changed maxAllowablePoints to 4. z is being shown as in figure below i dont know is it vector or not there is no space between elements
2019-06-03_10h16_57.png

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by