You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to fix the current axes in gui?
4 views (last 30 days)
Show older comments
I have a gui contains two axes, one of them are used to plot a mechanical motion for a certain mechanisms over a period of time, and when the animation starts the plot start to apear at axes1, but if I click at axes2 the animation start to appear at it, it seems that when any axes get clicked, it becomes the current one.
the problem is that I want the animation always appear at axes1 no matter if I clicked at any other axis, but I have to mention something important that I can't use plot(handels.axes1,.....,.....) or axes(handles.axes1).
is there is a command that can off or inactice a certain axes?
like this command which could off a certain pushbutton, set(handles.pushbutton1,'enable','off').
19 Comments
Rik
on 2 Apr 2020
Why can't you use plot(handels.axes1,.....,.....)? That is exactly the method to use. Another option is to create the line object with plot and then modify its properties:
h_plot=plot(x,y,'--r','Parent',handels.axes1);
%in your loop that does the animation:
set(h_plot,'XData',new_x,'YData',new_y)
The benefit of that latter structure is that is much faster than deleting and recreating grafics objects.
Osama Alkurdi
on 2 Apr 2020
I didn't get it.
the animation is created using functions and not plot commands in callback function.
Walter Roberson
on 2 Apr 2020
Why not modify the functions that create the animation so that they use graphics properly?
Search for tag:always-parent for a description of the code changes that need to be made.
Osama Alkurdi
on 2 Apr 2020
Edited: Osama Alkurdi
on 2 Apr 2020
@Rik
the callback function creates a temporary script file which contains a set of functions each on creates part of the motion, and I can't understand how your solution will work for me, and how I could implemnt it in my code, can you explain more clearly please.
Rik
on 2 Apr 2020
That sounds like a fragile and complex system that is extremely difficult to modify or debug. Why weren't those function written as normal functions? That way you should have been able to make the modifications.
Can you share your full code in an m file? You can attach it to your question.
Osama Alkurdi
on 2 Apr 2020
Edited: Osama Alkurdi
on 2 Apr 2020
@Rik
my program has alot of .m files, can you specify clearly the part you want me to attach.
I solved the problem by creating a ButtonDownFcn for axes2 which excuted when I press at it, and I write within it axes(handles.axes1) and that would set the current axes to axes1 if axes2 gets clicked, but I still want more elegant solution.
Rik
on 2 Apr 2020
I would like you to attach the relevant m files. Only you know which that would be. What I don't understand is how you ended up with a complex solution like writing a script to a file and running that, but figuring out how you can put in object handles into such code or judging which files are relevant is beyond you.
You should not be relying on any axes being the current axes for your GUI to work properly. You should be using direct object handles.
Osama Alkurdi
on 2 Apr 2020
Edited: Osama Alkurdi
on 2 Apr 2020
@Rik
"What I don't understand is how you ended up with a complex solution like writing a script to a file and running that"
this is the only solution that would success for my program, I ended with this solution because the number of the mechanisms which could be constructed is massive, so I figure out that I have to define a functions that perform the primary (translation, rotation, general) motion in dynamics and the user could add them togather in the number and in the order he wants to construct any mechanical mechanism.
"You should not be relying on any axes being the current axes for your GUI to work properly. You should be using direct object handles."
but I can't input handles gui axes in function parameters, the generated code below depends on the mechanism it self (each mechanical mechanism have its parts of links and pin support and gears )
the script below will be executed when a pushbutton gets clicked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t_instantaneous=0:1/25:9-1/25
% the two functions below are plotting two links each one move in a certain way
%I have to mention that there is alot of functions like them each one could perform of a certain kind of dynamic motion
tovlp01(0,2,[2,2*pi/3,0],4,pi/4,t_instantaneous)
tovlp01(-5,1,[5,0,0],3,pi,t_instantaneous)
%%%%%
axis([-10,10,-10,10])
pbaspect([1,1,1])
pause(1/25)
hold off
end
beep
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Osama Alkurdi
on 2 Apr 2020
this is the function used in the above script
Rik
on 2 Apr 2020
Why is there a call to clear and clc in there? That doesn't make sense in the context of a function. A function will keep its own workspace clean, so you don't have to use clear. clc only makes sense if you are printing text or warnings to the command window, which is not happening in this code.
I see no reason in the code you posted why you couldn't put in an extra input argument for the target axes. Both the plot and hold functions allow you to specify the parent axes. The axis and pbaspect functions also allow you to specify the target axes.
Adam Danz
on 2 Apr 2020
To triple-down on what Rik & Walter suggested, always use the parent handles in graphics functions.
fig = figure();
ax = axes(fig);
plot(ax, . . .)
hold(ax, 'on')
hold on only needs to be executed once to continually hold the axes.
Osama Alkurdi
on 2 Apr 2020
the clear and clc are for something I want to experience and I am not intend to write them in the reply, I am so sorry
"I see no reason in the code you posted why you couldn't put in an extra input argument for the target axes. Both the plot and hold functions allow you to specify the parent axes"
I want to put the gui axes as an input argument and when I do it matlab say undefined handles.axes1
Adam Danz
on 2 Apr 2020
Edited: Adam Danz
on 2 Apr 2020
"I want to put the gui axes as an input argument and when I do it matlab say undefined handles.axes1"
Once you enter a function, you are in that function's workspace. Think of the very beginning of a function as an empty box. To grant access to the GUI's axes handle, pass the handle into the function (there are other methods to get access to the handle but they are inferior).
function tovlp01(X_p_o_t_p_p,m_m_t,c_v,l_l,s_a,t_instantaneous, axisHandle)
% add this ^^^^^^^^^^
plot(axisHandle, . . .)
hold(axisHandle, 'on')
end
Osama Alkurdi
on 3 Apr 2020
@Adam Danz
I have two axes in my gui, so this (axisHandle) will handle any axes of them ???
Osama Alkurdi
on 3 Apr 2020
matlab give me undefined handles.axes1 !!!!!!
Rik
on 3 Apr 2020
In GUIDE generated code your callbacks will have the handles matrix available. If you decide to take the unfortunate route of doing things like calling clear at the beginning of the function you can get it back with this:
handles=guidata(gcbf);
Osama Alkurdi
on 4 Apr 2020
@Rik
@Adam Danz
@Walter Roberson
Thank you Guys :)
I finally understand what you mean after studying your comments and searching in the web, @Rik your solution work perfectly for me, and I know how to implement it in my code.
I learned a new things in matlab, thank you again.
Answers (0)
See Also
Categories
Find more on Specifying Target for Graphics Output in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)