2 questions regarding text() function in a FOR loop for a CURRENT graph.How to define CURRENT graph?
8 ビュー (過去 30 日間)
古いコメントを表示
Hi guys I have big difficulty adding text to a figure in a FOR LOOP in my function. when the execution of my function is finished;figure (100) is opened, and i use the same below FOR LOOP and it works out and prints out numbers (variable k) in those specific locations.but when I add the same FOR loop in my function, it doesn't print out any thing. Any help would be really appreciated. here is my function:
function [ ]=par1()
figure(100);
plot([2;3;4],[1;0;5]); // the first graph
hold on
plot([0;1;3],[6;4;7],'Color','c'); //the second graph
for k=1:+1:10
figure(100);
text(k,0.5,num2str(k));
drawnow;
end //end of for Loop
end //end of Function
I tried replacing line 7 (figure(100)) outside the For loop , but it didn't work out then I removed line 9, but it doesn't change anything either.
My other question is in the same regard. I wanted to change the XTickLabel of figure(100) to other values in my same function.So i added this line after line 10 (end of for Loop)
set(gca,'XTickLabel',[0:+60:240])
it doesn't work. but if i run these lines after executing my function, it works perfectly fine(meaning the k values appear in my figure). so it seems to me that in both problems, I don't correctly state to the program which figure is the CURRENT one or which AXE is the current Axe. but How can i tell it to my function.
3 件のコメント
Jan
2012 年 9 月 12 日
編集済み: Jan
2012 年 9 月 12 日
Did you use the debugger already and set a breakpoint inside the FOR loop? Are there problems with the "+1" in "for k=1:+1:10"? Try "for k=1:10" instead.
You cannot change "XTickLabel of figure(100)", because the TickLabels belong to an AXES, not to a FIGURE.
回答 (1 件)
Matt Tearle
2012 年 9 月 12 日
I'm having difficulty finding the problem. This works when I run it. Are you not seeing any numbers on your graph? (You won't see all of them because some are outside the current axis limits and text doesn't resize the axes. That much is expected behavior.)
Either way, I think the best answer to your question "How can i tell [which figure is the CURRENT one] to my function" is to keep the handles to graphics objects and use them, in conjunction with the Parent property:
hfg = figure(100);
hax = axes('Parent',hfg);
% do stuff
text(...,'Parent',hax)
% do more stuff
set(hax,'XTickLabel',...)
% etc
plot(hax,...) % plots directly into the specified axes
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Object Properties についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!