Two lines of plot title with number and date time
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(4 个)
Star Strider
2024-10-20
编辑:Star Strider
2024-10-20
Try this —
load('title_data.mat')
whos('-file','title_data.mat')
% 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 个评论
Star Strider
2024-10-20
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')
filename = 'St-01_datamine_short.xlsx';
T1 = readtable(filename)
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
2024-10-20
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.
埃博拉酱
2024-10-20
编辑:埃博拉酱
2024-10-21,0:59
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)));
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 个评论
Binaya
2024-10-20
编辑:Binaya
2024-10-20
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});
For more details on this please refer to the following documentation link: https://www.mathworks.com/help/matlab/ref/title.html#btpi3rq-3:~:text=%27center%27%3B-,Input%20Arguments,-collapse%20all
I hope this helps solve your query.
0 个评论
the cyclist
2024-10-20
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 个评论
Walter Roberson
2024-10-20
filename = 'St-01_title_data.mat';
fileprefix = regexp(filename, '^[^_]*', 'match');
title_text = sprintf("%s Depth: %d ± %d m", fileprefix, depth_mean, std_depth);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!