how to fix the current axes in gui?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2020-4-2
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
2020-4-2
I didn't get it.
the animation is created using functions and not plot commands in callback function.
Walter Roberson
2020-4-2
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.
Rik
2020-4-2
What functions are you using? Most internal Matlab functions will allow you to specify the parent axes and all custom functions should take advantage of this.
Osama Alkurdi
2020-4-2
编辑:Osama Alkurdi
2020-4-2
@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
2020-4-2
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
2020-4-2
编辑:Osama Alkurdi
2020-4-2
@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
2020-4-2
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
2020-4-2
编辑:Osama Alkurdi
2020-4-2
@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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Rik
2020-4-2
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
2020-4-2
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
2020-4-2
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
2020-4-2
编辑:Adam Danz
2020-4-2
"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
2020-4-3
@Adam Danz
I have two axes in my gui, so this (axisHandle) will handle any axes of them ???
Rik
2020-4-3
Just pass the appropriate handle when you call the function:
tovlp01(0,2,[2,2*pi/3,0],4,pi/4,t_instantaneous,handles.axes1)
Rik
2020-4-3
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
2020-4-4
@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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)