Two lines of plot title with number and date time
조회 수: 12 (최근 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일
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!