Two lines of plot title with number and date time

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)

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

Thank you @Star Strider! to complete the solution, to deal with an excel file, I use this code but it doesn't work for the moment.
T1 = readtable('St-01_datamine_short.xlsx');
load("St-01_title_data.mat");
figure;
plot(T1.value_y);
fileseg = string(extractBefore(T1,'_')); % this line needs adjustment, please suggest
titletext = fileseg+" Depth: "+depth_mean+" ± "+std_depth+" m" ;
subtitletext =string(dtime1)+" to "+string(dtime2);
title(titletext)
subtitle(subtitletext)
Any suggestion for the line: fileseg = string(extractBefore(T1,'_')) ? I think I shouldn't use extractBefore straight forward.
I think I fixed it...
filename = 'St-01_datamine_short.xlsx';
T1 = readtable(filename);
filename_str = convertCharsToStrings(filename);
load("St-01_title_data.mat");
figure;
plot(T1.value_y);
fileseg = string(extractBefore(filename_str,'_')); % this line needs adjustment
titletext = fileseg+" Depth: "+depth_mean+" ± "+std_depth+" m" ;
subtitletext =string(dtime1)+" to "+string(dtime2);
title(titletext)
subtitle(subtitletext)
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)
.
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.

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

Sure. I did it. But then what should I write on text of the title? That was my question indeed.
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.
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

Thank you @the cyclist @Star Strider @Binaya! All worked fine for me. My next following question, is it possible to call part of the file name and make it part of the title? I attached new file "St-01_title_data.mat". So, in this case, I want to get the first line of the title becomes St-01 Depth 746 ± 8 m. Any suggestion?
Another question, if the case is the file has another format (let's say in xlsx format) and I use readtable to call it, will it be having the same solution as you will suggest?
Thanks!
filename = 'St-01_title_data.mat';
fileprefix = regexp(filename, '^[^_]*', 'match');
title_text = sprintf("%s Depth: %d ± %d m", fileprefix, depth_mean, std_depth);
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.

Categories

Products

Release

R2022a

Asked:

on 20 Oct 2024

Edited:

on 21 Oct 2024

Community Treasure Hunt

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

Start Hunting!