我有一個Matlab GUI代碼,可以讓您在軸上繪圖並將座標傳遞給Simulink中的常量。當按住鼠標按鈕時,它應該繪製座標軸併發送座標,當它不是時,它應該發送座標但不應繪製。下面是代碼: `matlab set_param函數在循環中不起作用
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global bool;
bool=false;
set(handles.figure1,'WindowButtonMotionFcn',@(hObject,eventdata)figure1_WindowButtonMotionFcn(hObject,eventdata,guidata(hObject)));
%set the WindownButtonMotionFcn back in order to make it work again
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
set(handles.figure1,'WindowButtonMotionFcn',@empty); %change the windowbuttonmotionfcn in order not to let it work
global bool;
bool=true;
global lastX;
global lastY;
x=0;
while bool
coord=get(handles.axes4,'CurrentPoint');
if coord(1)<0.003
coord(1)=0.003
x=0;
end
if coord(1)>1
coord(1)=1
x=0;
end
if coord(3)<0
coord(3)=0
x=0;
end
if coord(3)>0.95
coord(3)=0.95
x=0;
end
if x>1
arrayX=[lastX coord(1)];
arrayY=[lastY coord(3)];
line(arrayX,arrayY);
set_param('dosya_yukle_deneme/Constant','value',num2str(coord(1)));
end
x=x+1;
lastX=coord(1);
lastY=coord(3);
drawnow;
end
function empty(~,~,~)
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
coord=get(handles.axes4,'CurrentPoint');
set_param('dosya_yukle_deneme/Constant','value',num2str(coord(1)));
當鼠標按鈕被按下時,它繪製的線條,但set_param功能不起作用。但是,figure1_WindowButtonMotionFcn中的那個在需要時工作得很好。看起來問題是while循環。任何幫助,將不勝感激。
這不是一個bug;這是因爲while循環阻止了回調的返回。 Matlab GUI是單線程的,所以如果回調沒有返回,那麼GUI被阻塞,並且不能正確更新 - 參見回答 – Tom
@Tom好吧,它實際上工作。但起初,我並沒有想到它也能工作:D –