开发者

Updating GUI axes in MATLAB

I am making a GUI for a program I have created, where I need to be able to change the position of a load along a beam. I have set up the axes and the slider properly, but I am unsure how to get the axes to update, as I can't find any examples that show how to do this on the internet.

At present, as I move the load, the position updates properly, but the old position stays on screen as well, which开发者_C百科 is quite annoying.

Can anyone recommend any good examples that show how to do this, or does anyone have a suggestion of how to go about refreshing the axes?


Here is the slider callback (I haven't included the create_fcn function). Also, theres a lot of comments in the code, as I used the Guide function to make the GUI.

Please note that the input to slider is a proportion of the overall beam length (as a decimal).

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);

Obviously, I would like to keep the other lines drawn, but change PL1Draw as the slider is moved. Please can you explain what I am supposed to tag to do this?

Many thanks in advance.

James


I assume that you have plotted a beam, which is supposed to bend as you change the slider value. Since you are able to plot the new position into the axes, I assume that you know how to write callbacks. I further assume that there are parts of the plot that should stay the same, and parts that should change.

To change the parts that need changing, the easiest method is just to delete them and then to redraw. In order to delete specific items from a plot, it's best to tag them. Thus, your plotting would go like this

%# 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

To make the GUI respond faster (if needed), do not delete and re-plot, but change the 'XData' and 'YData' property of the old position object.

Note 2

If you aren't already doing so, put the plotting function (the one that updates everything in the plot, not just the position of the load) in a separate function, not into the callback of the slider, and have instead the slider callback call the plot function. This way, you can call the same plotting function from several buttons and sliders, which makes the code a lot easier to maintain.

EDIT

I have updated the commands. Note that there is no special 'tagging' function. 'Tag' is a property of every graphics object, like 'Units', or 'Color'. It simply helps you to label the graphics objects so that you don't need to remember the handles.


Unrelated to the actual question, but related to the project:

http://www.mathworks.com/matlabcentral/fileexchange/2170

This book is still available used on Amazon, it might save you a lot of the coding of the Mechanics of Materials stuff. I wrote this about 12 years ago as an undergrad, but I think the MATLAB code should still be functional.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜