tags:

views:

252

answers:

3

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 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?

+1  A: 

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.

Jonas
Jonas, thanks for your response, but I don't fully understand how to use the tagging functions you specified (This is my first attempt at making a GUI. Please see my answer below, with some of my source code (I couldn't put all the code in the window). Hopefully this will clear things up a bit.
James
A: 

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

James
It is better to edit your question than to add another answer, since while editing my answer, I cannot see other answers.
Jonas
Also note that it might be a good idea to have an input variable like 'initialize' in the function call to LoadPlotter, so that you don't need to redraw the beam every single time you change the slider position.Instead, you'd run "if initialize,%draw beam%,end"
Jonas
A: 

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.

MatlabDoug
Thanks, thats really helpful.
James