How to join variables in the title of plot?

I am using the matlab lines as follows
title(['PCA scores', method, par]);
where method and par keep on changing for different plots. I want to join 'PCA scores' with method and par in one line. I am attaching the title of plot and request you to kindly suggest me how to join them in one line.
Deva

3 commentaires

Devendra
Devendra le 18 Avr 2024
I have changed it to
title(['PCA scores', num2str(method),num2str(par)]);
however it is giving following error
Error using num2str (line 24)
Input to num2str must be numeric
Deva
What are the data types of method and par?
Devendra
Devendra le 18 Avr 2024
Modifié(e) : Devendra le 18 Avr 2024
Method is LR or RF Par is Ratoon or Plant Please suggest me how to print these variables into title of plot in one line?
Thanks for your kind cooperation.
Deva

Connectez-vous pour commenter.

Réponses (2)

VBBV
VBBV le 18 Avr 2024
title(['PCA scores', num2str(method), num2str(par)]);

8 commentaires

VBBV
VBBV le 18 Avr 2024
Assuming method & par are numeric values that change/take different values
Devendra
Devendra le 18 Avr 2024
No actually they are names of method and parameters like Method may be LR or RF Par may be ratoon or plant Thanks for your help,
Deva
Devendra
Devendra le 18 Avr 2024
They are being printed in title as shown in attached figure. But they are not printed in one line. I want to print them in one line.
Deva
VBBV
VBBV le 18 Avr 2024
Modifié(e) : VBBV le 18 Avr 2024
If they are not numeric, then you could use string and set the FontSize to 8 (smaller) if the text in method and par variables are long enough .
title([string(PCA scores), string(method), string(par)],'FontSize', 8); %
Devendra
Devendra le 18 Avr 2024
Thank you very much for your help. It is still not printing in one line.
Deve
Devendra
Devendra le 18 Avr 2024
I have done little modification as
title(['PCA scores'+ string(method)+string(par)],'fontsize',14);
it worked.
Thanks for your help.
Deva
Devendra
Devendra le 18 Avr 2024
My rmse error is printed as follows
RMSE = 3.1e+02
I want to print it like
RMSE = 310
Please suggest me how to do it.
Deva
use
format shortG

Connectez-vous pour commenter.

Hi Devendra,
To join 'PCA scores' with variables `method` and `par` in one line for a MATLAB plot title, you can use the `sprintf` function or direct string concatenation. Here are two concise examples:
Using `sprintf`:
title(sprintf('PCA scores %s %s', method, par));
Using String Concatenation (MATLAB R2016b and later):
title(['PCA scores ' method ' ' par]);
Example:
method = 'LR';
par = 'Ratoon';
% 1st method
title(sprintf('PCA scores %s %s', method, par))
%2nd method
title(['PCA scores ' method ' ' par])
Thanks

Catégories

En savoir plus sur Mathematics and Optimization dans Centre d'aide et File Exchange

Produits

Version

R2024a

Question posée :

le 18 Avr 2024

Commenté :

le 18 Avr 2024

Community Treasure Hunt

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

Start Hunting!

Translated by