Matlab plot legend automation -


i solving pde depends on x , t , show solution on x few values of t. trying write code automatically generate legend plot. example, if plotting solution @ t = 0, 1, 5, , 9, want legend show "t=0", "t=1", , on.

let's solution held in matrix u. times held in vector t. index of times sampling in vector tsampled. note not same time want on plot. if take time @ index 6 of vector t, value not 6, anything.

i attempting by:

tlen = max(size(t)); tsampled = [2, floor(tlen/5), floor(2*tlen/5), floor(3*tlen/5), floor(4*tlen/5), floor(tlen)]; t(tsampled) legnd = {'', '', '', '', '', ''}; hold on = 1:1:size(tsampled)  plot(x,u(tsampled(i),:)) legnd(i) = sprintf('t = %0.2f s \n', t(tsampled(i))); end title('my pde solution'); legend(legnd, 0); xlabel('x') ylabel('u') hold off 

but producing error "conversion cell char not possible."

when instead try using line:

legend (sprintf('t = %0.2f s \n', t(tsampled))) 

i correct "strings" on graph, formatted this: enter image description here

i show "t=10.20 s" next blue line, "t = 91.84 s" next orange line, , on. how result want?

because predefined legnd cell array, need use {} instead of () correct index. try:

legnd{i} = sprintf('t = %0.2f s \n', t(tsampled(i))); 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -