How do I insert a variable text string in matlab plot?

Part of my code is
d_close = 3*(r1_eq + r2_eq)
figure(101);
h1 = plot(normalized_time, r1_us, 'b-', normalized_time, r2_us, 'k:');
set(h1,'linewidth',2);
txt = ['d = ', num2str(d_close)];
% t = text(0.0025, 17, txt);
t.FontSize = 24;
legend(['Bubble-1'], ['Bubble-2'])
and so on...
I wish to put sort of additional "third" legend for a plot that has two curves (r1_us and r2_us versus normalized time) only. So, I had to opt for string-method to put the additional information on the plot. But when I run the code, I get the error
Unable to perform assignment because dot indexing is not supported for variables of this type.
Error in bubble_mettin_solver (line 25)
t.FontSize = 24;
So how do I fix this? Please help. I wish that every time the plot shows up it has the information of updated d_close value.
By the way can I have a third legend in which there are only two curves? I know how to implement variable legend, but not the text.

6 件のコメント

madhan ravi
madhan ravi 2018 年 11 月 13 日
upload datas of r1_eq and r2_eq
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
The actual code is this:
clc;
clear all;
%close all;
time_range = [0 0.6d-4];
r1_eq = 10d-6;
r2_eq = 10d-6;
f_s = 20000;
initial_conditions = [r1_eq 0.d0 r2_eq 0.d0];
[t, r] = ode45('bubble_mettin', time_range, initial_conditions);
r1_us=1000000*r(:,1);
r1dot=r(:,2);
r2_us=1000000*r(:,3);
r2dot=r(:,4);
max_radii_combined = (max(r2_us) + max (r1_us))/1d6
d_close = 3*(r1_eq + r2_eq)
d_far = 300*(r1_eq + r2_eq)
diff_close_safe_positive = d_close - max_radii_combined
diff_far_safe_positive = d_far - max_radii_combined
figure(101);
normalized_time = t./(1/f_s);
h1 = plot(normalized_time, r1_us, 'b-', normalized_time, r2_us, 'k:');
set(h1,'linewidth',2);
txt = ['d = ', num2str(d_close)];
% t = text(0.0025, 17, txt);
t.FontSize = 24;
legend(['Bubble-1'], ['Bubble-2'])
legend('Location','Northwest')
set(gca,'xscale','linear','FontSize',26)
set(gca,'yscale','linear','FontSize',26)
set(gca,'XMinorTick','on')
set(gca,'YMinorTick','on')
%xlim([0 1]);
%ylim([0 24]);
%axis 'auto x'
grid off
xlabel('Normalized time, $t/T_{s}$','interpreter','latex','FontSize',34);
ylabel('Radii, $R_{1,2 \mbox{ }} (\mu m)$','interpreter','latex','FontSize',34)
hold off
madhan ravi
madhan ravi 2018 年 11 月 13 日
bubble_mettin?
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
I have partly fixed it myself using: But how do I latex the fonts and also how do I increase its size.
text(0.2, 21, sprintf('d = ', num2str(d_close)));
text.FontSize = 34;
text.Fontsize does not seem to work.
Stephen23
Stephen23 2018 年 11 月 13 日
Get rid of these square breckets:
legend(['Bubble-1'], ['Bubble-2'])
They do nothing useful whatsoever:
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
Agreed. removed but currently I am facing another issue.

サインインしてコメントする。

 採用された回答

madhan ravi
madhan ravi 2018 年 11 月 13 日
編集済み: madhan ravi 2018 年 11 月 14 日

0 投票

t=text(0.2, 21, ['d = ' num2str(d_close)]);
t.FontSize = 34;
Vikash pandey's solution:
txt = text(0.03, 21, ("$d =\mbox{ }$" + d_close*1d6 + "$\mu m$"),'Interpreter','latex');
txt.FontSize = 28;

19 件のコメント

Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
編集済み: Vikash Pandey 2018 年 11 月 13 日
Tried:
txt = texlabel('d = ', d_close);
text(0.2,20,txt)
did not worked. The value of d is missing.
madhan ravi
madhan ravi 2018 年 11 月 13 日
編集済み: madhan ravi 2018 年 11 月 13 日
txt = texlabel('d = ', d_close);
should be
txt = texlabel(['d = ', num2str(d_close)]);
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
Thanks Madhan. Appreciated.
madhan ravi
madhan ravi 2018 年 11 月 13 日
Anytime :)
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
編集済み: Vikash Pandey 2018 年 11 月 13 日
Madhan, What if I wish to have mu m in latex (greek letters) after the value of d_close?
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
I tried:
txt = text(0.05, 21, texlabel(['d = ' num2str(d_close) 'mu m']));
Did not worked.
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
編集済み: Vikash Pandey 2018 年 11 月 13 日
Update: I got it to work partly
txt = text(0.05, 21, (['d = ' num2str(d_close), '\mu m']));
txt.FontSize = 24;
But, "m" does not look like a mathematical character as it looks neat in latex. Here in Matlab it looks like just a regular alphabetic letter. I wish mu and m to appear as they appear from latex coding $\mu m$ in the respective pdf file.
madhan ravi
madhan ravi 2018 年 11 月 13 日
yes where were we? so you want to display mu and m?
madhan ravi
madhan ravi 2018 年 11 月 13 日
編集済み: madhan ravi 2018 年 11 月 13 日
There you go!
a='\mu','Interpreter','latex'
txt = text(0.05, 21, ['d = ' num2str(d_close), a,'m']);
txt.FontSize = 24;
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
tried, No change at all. Same as before. "m" looks like regular alphabet.
madhan ravi
madhan ravi 2018 年 11 月 13 日
編集済み: madhan ravi 2018 年 11 月 13 日
by the way how does your variable m is in latex form in the(ylabel) picture below?
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
編集済み: Vikash Pandey 2018 年 11 月 13 日
So no remedy for latex letters in text string?
For the labels, I used this:
xlabel('Normalized time, $t/T_{s}$','interpreter','latex','FontSize',34);
ylabel('Radii, $R_{1,2 \mbox{ }} (\mu m)$','interpreter','latex','FontSize',34)
madhan ravi
madhan ravi 2018 年 11 月 13 日
編集済み: madhan ravi 2018 年 11 月 13 日
Guess so ,did a lot of research but couldn't succeed though!
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
It happens. It is okay, not a big deal so far. I was looking for that only for aesthetic purposes. By the way I have posted one more problem in case it interests you.
https://www.mathworks.com/matlabcentral/answers/429660-how-do-i-output-the-second-derivative-from-ode-solver-for-further-use
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
FYI, I found an alternative way:
txt = text(0.03, 21.5,'$$d = 33 \mu m$$', 'interpreter','latex')
txt.FontSize = 28;
madhan ravi
madhan ravi 2018 年 11 月 13 日
cool but you dont get to use num2str do you?
Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
Nope, num2str makes it difficult to work. But I have to get 10 plots or so. I will do hard-coding as shown above to obtain relatively good plots. By the way, had a look at this: https://www.mathworks.com/matlabcentral/answers/429660-how-do-i-output-the-second-derivative-from-ode-solver-for-further-use
?
Vikash Pandey
Vikash Pandey 2018 年 11 月 14 日
Madhan. I found the solution to the problem from a user in Stackoverflow. The solution is:
txt = text(0.03, 21, ("$d =\mbox{ }$" + d_close*1d6 + "$\mu m$"),'Interpreter','latex');
txt.FontSize = 28;
madhan ravi
madhan ravi 2018 年 11 月 14 日
wow that's cool!

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2018 年 11 月 13 日

2 投票

Start with omitting the clear all, because this is a waste of time only. The error message is clear: Your t is the output of ode45() and a double vector. Then you cannot define t.FontSize. In the line before, the t would have been re-defined, but it is commented:
% t = text(0.0025, 17, txt);
Either delete the "%" character there or better use a unique name for the variable:
TestH = text(0.0025, 17, txt);
TextH.FontSize = 24;
It is not clear, what a "third legend" is. There is one legend only and not even a second one. Do you want to get a third entry in this legend? This would be confusing, because readers expect the same number of lines as legend entries. Maybe an additional title helps: https://www.mathworks.com/matlabcentral/answers/324848-setting-a-title-for-a-legend

1 件のコメント

Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日
Thanks for the advise. I corrected it. But still a minor issue remains. Please see above.

サインインしてコメントする。

Vikash Pandey
Vikash Pandey 2018 年 11 月 13 日

0 投票

Yes. you see the plot here, mu looks so different than m in the plot. I want them to be latex equally. By the way I have solved the bubble coupled ODE problem that I had posted yesterday. Hope you remember. :)
Similar_Size_Far_Radius.png

カテゴリ

質問済み:

2018 年 11 月 13 日

編集済み:

2018 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by