我正在为我所创建的程序制作一个GUI,在那里我需要能够改变梁上负载的位置。我已经正确地设置了轴和滑块,但我不知道如何使轴更新,因为我找不到任何例子显示如何在互联网上这样做。
目前,当我移动负载,位置更新正常,但旧的位置也停留在屏幕上,这是相当恼人的。
有人能推荐一些好的例子来说明如何做到这一点吗?或者有人有关于如何刷新轴的建议吗?
下面是滑块回调(我还没有包含create_fcn函数)。此外,在代码中有很多注释,因为我使用Guide函数来生成GUI。
请注意,滑块的输入是整个光束长度的一个比例(作为十进制)。
function PointLoadxx1posslider_Callback(hObject, eventdata, handles)
% hObject handle to PointLoadxx1posslider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
PLxx1pos = get(handles.PointLoadxx1posslider,'value');
set(handles.PLxx1posedit, 'String', num2str(PLxx1pos));
l = 3000; % This is the Length of the beam
zpl1 = get(handles.PointLoadxx1posslider,'value')*l;
% Multiplies the position decimal by the overall length
LoadPlotter(hObject,zpl1,handles) % Sends the command to the plot plot function
guidata(hObject,handles);
function LoadPlotter(hObject,zpl1,handles)
% The following draws the beam supports as lines
SH = l/20; %Height of supports
line([0 l], [SH/2 SH/2])
line([-SH/2 SH/2], [0 0])
line([-SH/2 0], [0 SH/2])
line([0 SH/2], [SH/2 0])
line([l-SH/2 l+SH/2], [0 0])
line([l-SH/2 l], [0 SH/2])
line([l l+SH/2], [SH/2 0])
xlim([ -100 l+200])
ylim([-l/2 l/2])
%Draw Load position
% zpl1 = get(handles.PointLoadxx1posslider,'value')*l;
% zpl1 = 0.5*l;
zpl2 = 0.2*l;
PL1 = 50;
%This is the value of the point load applied to the beam, which will
be an input from another slider
PL1Draw = line([zpl1 zpl1],[SH/2 PL1*10]);
% refresh(handles.axes1);
guidata(hObject,handles);
显然,我希望保留绘制的其他线条,但在移动滑块时更改PL1Draw。你能解释一下我该怎么做吗?
在此之前,非常感谢您。
詹姆斯
发布于 2010-03-24 07:56:56
与实际问题无关,但与项目有关:
http://www.mathworks.com/matlabcentral/fileexchange/2170
这本书仍然可以在亚马逊上使用,它可以为你节省大量的材料力学的编码。这是我12年前在读大学的时候写的,但我认为MATLAB的代码应该还能用。
发布于 2010-03-24 06:00:28
我假设你画了一个横梁,当你改变滑块值时,它应该弯曲。由于您能够将新位置绘制到轴中,所以我假设您知道如何编写回调。我进一步假设,地块的某些部分应该保持不变,有些部分应该改变。
要更改需要更改的部分,最简单的方法就是删除它们,然后重新绘制。为了从情节中删除特定的项目,最好给它们加上标签。这样,你的阴谋就会变成这样
%# remove the old position
%# find the handle to the old position by searching among all the handles of
%# the graphics objects that have been plotted into the axes
oldPosHandle = findall(handles.axes1,'Tag','position');
delete(oldPosHandle);
%# plot new position
PL1Draw = line([zpl1 zpl1],[SH/2 PL1*10]);
%# add the tag so that you can find it if you want to delete it
set(PL1Draw,'Tag','position');
Note 1
要使GUI响应更快(如果需要的话),不要删除和重新绘制,而是更改旧位置对象的'XData‘和'YData’属性。
Note 2
如果您还没有这样做,那么将绘图函数(更新绘图中所有内容的函数,而不仅仅是加载位置)放在单独的函数中,而不是放在滑块的回调中,而是让滑块回调调用绘图函数。通过这种方式,您可以从几个按钮和滑块调用相同的绘图函数,这使得代码更易于维护。
编辑
我已经更新了命令。请注意,没有特殊的“标记”功能。‘'Tag’是每个图形对象的属性,比如‘Unit’或'Color‘。它只是帮助您标记图形对象,这样您就不需要记住句柄了。
https://stackoverflow.com/questions/2508006
复制相似问题