How to use num2str inside text with latex interpreter?
    9 次查看(过去 30 天)
  
       显示 更早的评论
    
Hi to everyone, I want to assign a number to a string inside a text command to dynamically change the output of the text inside the plot. 
Here is an attempt of coding:
clc;clear all;close all;
figure
i=1
plot(1:6,2:7)
text(2,4,'$L_{1}$','Interpreter',"latex");
text(4,5,'$L_{',num2str(i),'}$','Interpreter',"latex");
Can you help me to correclty code the last line?
0 个评论
采纳的回答
  Star Strider
      
      
 2022-10-7
        I prefer to use sprintf for this.  (If you use any escape characters, you need to use double backslants.  See the documentation for detais.).  
figure
i=1
plot(1:6,2:7)
text(2,4,sprintf('$L_{%d}$',i),'Interpreter',"latex");
text(4,3,sprintf('$L_{\\frac{%d}{2}}$',i),'Interpreter',"latex");
.
4 个评论
更多回答(2 个)
  Jeff Miller
      
 2022-10-7
        I think you need square brackets like this to assemble all the characters into a single string
text(4,5,['$L_{',num2str(i),'}$'],'Interpreter',"latex");
0 个评论
  dpb
      
      
 2022-10-7
        i=1;
text(0.4,0.5,"L_{" + i + "}")
i=i+1;
text(0.4,0.4,"\it L_{" + i + "}")
i=i+1;
text(0.4,0.3,"\it L\rm_{" + i + "}")
Variations on a theme; the above doesn't require LaTeX; the default TeX interpreter can manage on its own.
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






