with cycle, changing the content of text

hi, matlab people
Is someone encountering the following case, or can anyone solve the following problem? any suggestion
I write a cycle to get lots of plots, at the same time I want to add text to these plots. The text content will change with the cycle.
thank you. help me please

댓글 수: 2

Fangjun Jiang
Fangjun Jiang 2011년 12월 31일
It can be done but I have no clue if you don't provide details.
petter
petter 2012년 1월 1일
thank you for your helpful suggestion.
the following is my code
syms f1;
A = [0.3*f1+3 , 0; 3 , 0; 3 , 0; 1 , 0];
j=0;
for m=1:2
for n=1:2
for o=1:2
for p=1:2
er=A(1,m);
ei=A(2,n);
ur=A(3,o);
ui=A(4,p);
str1='e:\photo3\photo';
j=j+1;
str=[str1 num2str(j) '.png'];
for d=1:0.5:5;
f=2:0.2:18;
e=subs(er-1i*ei,f1,f);
u=subs(ur-1i*ui,f1,f);
z = subs(abs(d*f*e*u^0.5),f1,f);
plot(f,z,':or')
hold on
end
name=[er ei;ur ui];% Here is my problem;I want to show text in figure with the content of name([er ei;ur ui]).
text(14,0.5, sprintf('Times=%d',name),'FontSize',18,'HorizontalAlignment','center','BackgroundColor',[.7 .9 .7]);
saveas(gcf,str);
close(gcf)
end
end
end
end
thank you again, please help my.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Tearle
Matt Tearle 2011년 12월 31일

1 개 추천

Something like this?
for k = 1:n
figure(k)
% get x and y
plot(x,y)
xbar = mean(x);
ybar = mean(y);
str = ['Average at x = ',num2str(xbar),', y = ',num2str(y)];
text(xbar,ybar,str)
end
sprintf is also useful for building a formatted string.
EDIT TO ADD: When I try to run your code I get a couple of problems. First, a matrix dimension problem due to f*e (instead of f.*e). Assuming that's just a typo, the next problem I run into is that sprintf doesn't accept symbolic inputs.
The simple fix to that is to convert sym to char. The bigger question is still what exactly you're hoping for as output. Do you want it to look like
Times = (3*f1)/10 + 3, 3
3, 1
If so, something like this might work
text(14,0.5, sprintf('Times=%s, %s\n%s, %s',char(er),char(ei),char(ur),char(ui)),'HorizontalAlignment','right')

댓글 수: 1

petter
petter 2012년 1월 1일
thank you matt tearle for your help.
According your suggestion, the code can run.
the code
syms f1;
A = [0.3*f1+3 , 0; 3 , 0; 3 , 0; 1 , 0];
j=0;
for m=1:2
for n=1:2
for o=1:2
for p=1:2
er=A(1,m);
ei=A(2,n);
ur=A(3,o);
ui=A(4,p);
str1='e:\photo3\photo';
j=j+1;
str=[str1 num2str(j) '.png'];
for d=1:0.5:5;
f=2:0.2:18;
e=subs(er-1i*ei,f1,f);
u=subs(ur-1i*ui,f1,f);
z = subs(abs(d.*f.*e.*u.^0.5),f1,f);
plot(f,z,':or')
hold on
end
name=[sprintf('er =%s\n',char(er)),sprintf('ei = %s\n',char(ei)),sprintf('ur = %s\n',char(ur)),sprintf('ui = %s',char(ui))];
%text(14,0.5, sprintf('Times=%d',name),'FontSize',18,'HorizontalAlignment','center','BackgroundColor',[.7 .9 .7]);
text(8,500, name,'FontSize',18,'HorizontalAlignment','left','BackgroundColor',[.7 .9 .7]);
saveas(gcf,str);
close(gcf)
end
end
end
end
thank you again.
another problem: in my code, I use 4 for cycle to carry out my aim, so the code run so slowly.
Is there any other way to carry out my aim??

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 31일

0 개 추천

The usual techniques. Construct your text with sprintf(), or with num2str() and with [] concatenation operations.
text(3,5, sprintf('You sunk my %s!', ShipName))

댓글 수: 2

%d is for numeric values. Use %s for strings.
It appears you probably want two different lines of text. How do you want the two lines arranged relative to each other? Perhaps
text(14,0.5, {sprintf('Times=%s', name(1,:), sprintf('______%s', name(2,:)}, ...)
except using spaces instead of underscores. On the other hand since you are not using a fixed-sized font, getting the two lines aligned is going to be difficult.
There is no real need to keep doing those symbolic calculations. You do not have anything messy like integration. Your z value can be relatively easily calculated numerically based upon the input values. You can set the calculation up once symbolically and create a numeric function from it using matlabFunction. You can create the inputs to pass to matlabFunction using ndgrid. Possibly you might need to use a bxsfun() or two to handle d and f.

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2011년 12월 31일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by