textbox doesnt like strings

%%Aufgabe 3
clear; clc; close all;
A3_ECE = "Daten\Student_2.xls";
A3_ECE_Data = readmatrix(A3_ECE);
[std_ECE,muh_ECE] = std(A3_ECE_Data(:,13));
i=1;
x_ece=A3_ECE_Data(:,2);
y_ece=A3_ECE_Data(:,14);
curve1 = y_ece + std_ECE;
curve2 = y_ece - std_ECE;
nl = newline;
x2 = [x_ece; flipud(x_ece)];
inBetween = [curve1; flipud(curve2)];
x2 = fillmissing(x2, 'linear'); % Fill 'NaN' Values
inBetween = fillmissing(inBetween, 'linear'); % Fill 'NaN' Values
abw = max(A3_ECE_Data(:,14)-A3_ECE_Data(:,13));
% Gesamtzeit für +/- 2km/h berechnen
g_zeit = 0;
d_zeit = 0;
t1 = 0;
for k = 1:1:length(A3_ECE_Data)
vsoll = A3_ECE_Data(k,14);
vroll = A3_ECE_Data(k,13);
t2 = A3_ECE_Data(k,2);
if (vsoll - vroll) > 2 || (vsoll - vroll) < -2
d_zeit = t2 - t1;
g_zeit = g_zeit + d_zeit;
end
t1 = t2;
end
figure(1);
fill(x2,inBetween, 'c', 'EdgeColor', 'c', 'FaceAlpha', 0.1);
hold on;
grid on;
line(x_ece, y_ece,'LineWidth',2);
title('Aufgabe 3: Geschwindigkeitsabweichung');
ylabel('Geschwindigkeit in [km/h]');
xlabel('Zeit in [ms]');
std_label = strcat("Standartabweichung ",num2str(std_ECE,'%.2f')," km/h");
g_zeit = g_zeit/1000;
g_zeit = num2str(g_zeit,'%.2f');
abw = num2str(abw,'%.1f');
info = {"Datei: " A3_ECE "max. Abweichung[km/h]:" abw "Gesamtzeit außerhalb +/- 2km/h [s]:" g_zeit };
inf_label = string(info);
legend(std_label , 'v_{Soll}', 'Location', 'best');
annotation('textbox','String',inf_label,'FitBoxToText','on','VerticalAlignment','top');
Hello, its me again,
so my main problem is that the annatation at the end shows up somewhere in my graph. unfortunatly verticalaligment doesnt work.
secondary problems are that i dont get how strings work. i want
'datei: Daten/student2.xls
max. Abweichung: xxx km/h
Gesamtzeit: xxx s
'
somehow i get a newline after every string. the function newline wasnt working well..
thanks in advance

 Accepted Answer

The string array properties are a bit different from cell arrays in that string arrays can be grouped using square brackets [] while cell arrays require curly brackets {}. Also, although I thought that square brackets would concatenate strings so that they would appear on the same line, apparently separate strcat calls are necessary for that.
See if this works the way you want it to —
%%Aufgabe 3
clear; clc; close all;
% A3_ECE = "Daten\Student_2.xls";
A3_ECE = "Student_2.xls";
A3_ECE_Data = readmatrix(A3_ECE);
[std_ECE,muh_ECE] = std(A3_ECE_Data(:,13));
i=1;
x_ece=A3_ECE_Data(:,2);
y_ece=A3_ECE_Data(:,14);
curve1 = y_ece + std_ECE;
curve2 = y_ece - std_ECE;
nl = newline;
x2 = [x_ece; flipud(x_ece)];
inBetween = [curve1; flipud(curve2)];
x2 = fillmissing(x2, 'linear'); % Fill 'NaN' Values
inBetween = fillmissing(inBetween, 'linear'); % Fill 'NaN' Values
abw = max(A3_ECE_Data(:,14)-A3_ECE_Data(:,13));
% Gesamtzeit für +/- 2km/h berechnen
g_zeit = 0;
d_zeit = 0;
t1 = 0;
for k = 1:1:length(A3_ECE_Data)
vsoll = A3_ECE_Data(k,14);
vroll = A3_ECE_Data(k,13);
t2 = A3_ECE_Data(k,2);
if (vsoll - vroll) > 2 || (vsoll - vroll) < -2
d_zeit = t2 - t1;
g_zeit = g_zeit + d_zeit;
end
t1 = t2;
end
figure(1);
fill(x2,inBetween, 'c', 'EdgeColor', 'c', 'FaceAlpha', 0.1);
hold on;
grid on;
line(x_ece, y_ece,'LineWidth',2);
title('Aufgabe 3: Geschwindigkeitsabweichung');
ylabel('Geschwindigkeit in [km/h]');
xlabel('Zeit in [ms]');
std_label = strcat("Standartabweichung ",num2str(std_ECE,'%.2f')," km/h");
g_zeit = g_zeit/1000;
g_zeit = num2str(g_zeit,'%.2f');
abw = num2str(abw,'%.1f');
info = [strcat("Datei: ", strrep(A3_ECE,'_','\_')), strcat("max. Abweichung[km/h]: ", abw), strcat("Gesamtzeit außerhalb +/- 2km/h [s]: ", g_zeit) ];
inf_label = string(info);
legend(std_label , 'v_{Soll}', 'Location', 'best');
annotation('textbox','String',inf_label,'FitBoxToText','on','VerticalAlignment','top');
I also added some spaces and a strrep call so that the underscore in the file name would print as an underscore and not be interpreted as a TeX subscript operator.
.

7 Comments

thank you very much! do you know how i change the position of the annotation to the top?
it works so well, i love you
Thank you!
i dont want to bug you to much, but the position of the annotation is still awfull :(
No worries!
%%Aufgabe 3
clear; clc; close all;
% A3_ECE = "Daten\Student_2.xls";
A3_ECE = "Student_2.xls";
A3_ECE_Data = readmatrix(A3_ECE);
[std_ECE,muh_ECE] = std(A3_ECE_Data(:,13));
i=1;
x_ece=A3_ECE_Data(:,2);
y_ece=A3_ECE_Data(:,14);
curve1 = y_ece + std_ECE;
curve2 = y_ece - std_ECE;
nl = newline;
x2 = [x_ece; flipud(x_ece)];
inBetween = [curve1; flipud(curve2)];
x2 = fillmissing(x2, 'linear'); % Fill 'NaN' Values
inBetween = fillmissing(inBetween, 'linear'); % Fill 'NaN' Values
abw = max(A3_ECE_Data(:,14)-A3_ECE_Data(:,13));
% Gesamtzeit für +/- 2km/h berechnen
g_zeit = 0;
d_zeit = 0;
t1 = 0;
for k = 1:1:length(A3_ECE_Data)
vsoll = A3_ECE_Data(k,14);
vroll = A3_ECE_Data(k,13);
t2 = A3_ECE_Data(k,2);
if (vsoll - vroll) > 2 || (vsoll - vroll) < -2
d_zeit = t2 - t1;
g_zeit = g_zeit + d_zeit;
end
t1 = t2;
end
figure(1);
fill(x2,inBetween, 'c', 'EdgeColor', 'c', 'FaceAlpha', 0.1);
hold on;
grid on;
line(x_ece, y_ece,'LineWidth',2);
title('Aufgabe 3: Geschwindigkeitsabweichung');
ylabel('Geschwindigkeit in [km/h]');
xlabel('Zeit in [ms]');
std_label = strcat("Standartabweichung ",num2str(std_ECE,'%.2f')," km/h");
g_zeit = g_zeit/1000;
g_zeit = num2str(g_zeit,'%.2f');
abw = num2str(abw,'%.1f');
info = [strcat("Datei: ", strrep(A3_ECE,'_','\_')), strcat("max. Abweichung[km/h]: ", abw), strcat("Gesamtzeit außerhalb +/- 2km/h [s]: ", g_zeit) ];
inf_label = string(info);
ylim([min(ylim) 120])
legend(std_label , 'v_{Soll}', 'Location', 'best');
htb = annotation('textbox','String',inf_label,'FitBoxToText','on','VerticalAlignment','top');
Pos = htb.Position;
htb.Position = Pos + [-0.07 0.38 0 0];
The textbox has a Position property that you can change to put it (almost) anywhere you want it. Here, I also changed the ylim property of the plot to stretch its height to provide more room for the textbox and legend. (This is not required, I am only illustrating its use. That also changes the relative positions of the textbox and legend, since everything dynamically interacts.) The legend also has a Position property that you can use if you want to align it relative to the textbox, or the textbox relative to it.
Experiment with it to put it where you want it. I used relative position parmeters to define the textbox position (since that is easier for me to visualise), however defining absolute parameters is also possible. They are all relative to the plot limits.
.
dude (or dudette), i dont know what to say! thank you so much!!!
(Dude.)
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!