Two lines of plot title with number and date time

11 views (last 30 days)
Hello there,
Does anyone know how to set the title of a plot which contains two lines of information? Let's get into this example (I attached the informations I want to put as the title).
% Sample data
x = linspace(0, 3*pi, 200);
y = cos(x) + rand(1, 200);
% Create scatter plot
scatter(x, y);
load('title_data.mat')
text=['Depth :', num2str(depth_mean),' ± ',num2str(std_depth),' m'; "second line"];
title(text)
I did for the first line of the title (written as Depth : 746 ± 8 m. But, I still don't get the second line. I want the second line contain two datetime information, namely dtime1 and dtime2. My intention is to get the second line should be written as Feb-2021 to Dec-2023. The Feb-2021 comes from dtime1 and Dec-2023 comes from dtime2.
Thanks

Answers (4)

Star Strider
Star Strider on 20 Oct 2024
Edited: Star Strider on 20 Oct 2024
You cann do what yu want with string, " " arrays (or variables).
Try this —
load('title_data.mat')
whos('-file','title_data.mat')
Name Size Bytes Class Attributes depth_mean 1x1 8 double dtime1 - 24 datetime dtime2 - 24 datetime std_depth 1x1 8 double
% Sample data
x = linspace(0, 3*pi, 200);
y = cos(x) + rand(1, 200);
% Create scatter plot
scatter(x, y);
load('title_data.mat')
% text=['Depth :', num2str(depth_mean),' ± ',num2str(std_depth),' m'; "second line"];
filename = "St-01_title_data.mat";
fileseg = string(extractBefore(filename,'_'));
titletext = [fileseg+" Depth: "+depth_mean+" ± "+std_depth+" m" string(dtime1)+" to "+string(dtime2)];
title(titletext)
The space without an opeerator in the ‘titletext’ assignment creates a neew line. (I changed its name because text is a useful function you may want to use later.)
There are other options (specifically sprintf) if this does not work in R2022a, although it should.
EDIT — Forgot to add the ‘m’ unit. Added now.
EDIT — (20 Oct 2024 at 22:08)
Added:
filename = "St-01_title_data.mat";
fileseg = string(extractBefore(filename,'_'));
titletext = [fileseg+" Depth: "+depth_mean+" ± "+std_depth+" m" string(dtime1)+" to "+string(dtime2)];
to add the first part of the file name to the plot title.
Using an .xlsx file would simply require using readtable. It should automatically accommodate the file format and contents, although without having the file I cannot be certain that some other minor tweaks could be required.
.
  4 Comments
Star Strider
Star Strider on 20 Oct 2024
You were attempting to extract the file name segment from the table. You need to extract it from the file name instead. I created a separate variable for the file name. Other than that (and creating the whos call because I want to see the contents of the .mat file) your code is essentially unchanged.
Try this —
load('St-01_title_data.mat')
whos('-file','St-01_title_data.mat')
Name Size Bytes Class Attributes depth_mean 1x1 8 double dtime1 - 24 datetime dtime2 - 24 datetime std_depth 1x1 8 double
filename = 'St-01_datamine_short.xlsx';
T1 = readtable(filename)
T1 = 12x2 table
value_y time _______ ____________________ 12.887 15-Feb-2020 09:50:00 13.136 15-Feb-2020 10:00:00 13.127 15-Feb-2020 10:09:59 12.894 15-Feb-2020 10:20:00 12.816 15-Feb-2020 10:30:00 12.355 15-Feb-2020 10:39:59 12.317 15-Feb-2020 10:50:00 12.922 15-Feb-2020 10:59:59 13.162 15-Feb-2020 11:10:00 13.163 15-Feb-2020 11:19:59 13.109 15-Feb-2020 11:30:00 13.139 15-Feb-2020 11:39:59
figure;
plot(T1.time, T1.value_y);
fileseg = string(extractBefore(filename,'_')); % this line needs adjustment, please suggest
titletext = fileseg+" Depth: "+depth_mean+" ± "+std_depth+" m" ;
subtitletext =string(dtime1)+" to "+string(dtime2);
title(titletext)
subtitle(subtitletext)
.
Star Strider
Star Strider on 20 Oct 2024
My solution (of 27 minutes ago, 20 Oct 2024 at 23:02) seems to work at least in R2024b. I cannot test it in R2022a.

Sign in to comment.


埃博拉酱
埃博拉酱 on 20 Oct 2024
Edited: 埃博拉酱 on 21 Oct 2024
Use string to convert from datetime to string. Set necessary format and locale arguments as required.
20241021
Didn't you successfully add the second line title “second line”? Can't you just replace that text with your converted datetime string? Or are you unfamiliar with the concatenation among strings and character arrays?
title(sprintf('Depth: %u±%um%s%s to %s',depth_mean,std_depth,newline,string(dtime1),string(dtime2)));
Or you can add a subtitle.
For the filename extraction stuff, I like to use split:
Fields=split(filename,'_');
Prefix=Fields(1);
Use this method to easily extract any underscore-separated fields.
  2 Comments
Adi Purwandana
Adi Purwandana on 20 Oct 2024
Edited: Adi Purwandana on 20 Oct 2024
Sure. I did it. But then what should I write on text of the title? That was my question indeed.
埃博拉酱
埃博拉酱 on 21 Oct 2024
In that case, I guess you might need to learn about the sprintf and newline functions

Sign in to comment.


Binaya
Binaya on 20 Oct 2024
Edited: Binaya on 20 Oct 2024
Hi Adi
To insert two lines of text in the title of the plot you can pass a cell array into the 'title' function containing two strings: one for each line of the desired title. I have a provided an example code snippet below:
% Prepare the title text
depth_line = sprintf('Depth: %.2f ± %.2f m', depth_mean, std_depth);
date_line = sprintf('%s to %s', string(dtime1), string(dtime2));
% Set the title with two lines
title({depth_line, date_line});
I hope this helps solve your query.

the cyclist
the cyclist on 20 Oct 2024
Since R2020b, there is a subtitle function. Here is how I would have written your code, incorporating that and some other changes:
% Sample data
x = linspace(0, 3*pi, 200);
y = cos(x) + rand(1, 200);
% Create scatter plot
figure
scatter(x, y);
load('title_data.mat','depth_mean','std_depth','dtime1','dtime2')
title_text = sprintf("Depth: %d ± %d m",depth_mean,std_depth);
subtitle_text = sprintf("%s to %s",dtime1,dtime2);
title(title_text)
subtitle(subtitle_text)
  3 Comments
Walter Roberson
Walter Roberson on 20 Oct 2024
filename = 'St-01_title_data.mat';
fileprefix = regexp(filename, '^[^_]*', 'match');
title_text = sprintf("%s Depth: %d ± %d m", fileprefix, depth_mean, std_depth);
Adi Purwandana
Adi Purwandana on 20 Oct 2024
Thank you @Walter Roberson. Just tried your code and found this message "Error using sprintf
Function is not defined for 'cell' input"

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!