Title issues when plotting

4 visualizzazioni (ultimi 30 giorni)
John
John il 25 Lug 2025
Commentato: Image Analyst il 26 Lug 2025
I want my plot title to read
Plate (inputsheet)
Grain Size (Grain(n))
Varible
Varible
This is the code I am using. If it leave out 'Grain Size' it runs perfectly fine but I want to have the word Grain Size printed before the varible Grain(n) in the title.
title(['Plate ' InputSheet;'Grain Size' string(Grain(n));...
sprintf(['T_o_Q: %.1f' char(176) 'C (%.1f' char(176) 'F)'],T_oQlast (T_oQlast*1.8)+32);],...
'FontWeight','normal','FontSize',18,'FontName','Arial','Units','normalized',...
'Position',[0.02 0.98],'HorizontalAlignment','left','VerticalAlignment','top')
I get the following error
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in SEBPostTest (line 526)
title(['Plate ' InputSheet;'Grain Size' string(Grain(n));...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What am I doing wrong? I've spent the last hour and half trying to figure this out.
  4 Commenti
Stephen23
Stephen23 il 25 Lug 2025
Modificato: Stephen23 il 25 Lug 2025
"What am I doing wrong?"
Note what happens when you horizontally concatenate a character vector and a string scalar:
['hi',"world"]
ans = 1×2 string array
"hi" "world"
It does not turn into one character vector nor into a scalar string... you get two scalar strings!
Vertically concatenating a character vector with a string implicitly also converts it into a scalar string:
['hi';"world"]
ans = 2×1 string array
"hi" "world"
Now lets consider what this means for your attempt:
'Plate ' InputSheet;
'Grain Size' string(Grain(n));
sprintf(['T_o_Q: %.1f' char(176) 'C (%.1f' char(176) 'F)'],..)
which we know means that you are attempting to vertically concatenate:
1x1 string (1 character vector implicitly converted to string)
1x2 string (1 character vector implicitly converted to string, 1 string scalar)
1x1 string (1 character vector implicitly converted to string)
which clearly will not work, as those string arrays have different numbers of columns.
John
John il 25 Lug 2025
Thank you stephen23 this was confussing me!

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 25 Lug 2025
Modificato: Star Strider il 25 Lug 2025
Create everything as a string array --
InputSheet = "Plate #1";
Grain{1} = 42
Grain = 1×1 cell array
{[42]}
degsign = string(char(176));
n = 1;
T_oQlast = 84;
title(["Plate " + InputSheet "Grain Size " + Grain{n}, ...
string(sprintf(['T_o_Q: %.1f%sC (%.1f%sF)'],T_oQlast, degsign, (T_oQlast*1.8)+32, degsign))], ...
'FontWeight','normal','FontSize',18,'FontName','Arial','Units','normalized',...
'Position',[0.02 0.98],'HorizontalAlignment','left','VerticalAlignment','top')
Make appropriate changes to get the result you want.
EDIT -- (25 Jul 205 at 18:57)
'Grain(n) is a 1x1 cell'
Changed 'Grain' to a cell array.
.
  5 Commenti
Star Strider
Star Strider il 25 Lug 2025
Stephen, thank you.
Image Analyst
Image Analyst il 26 Lug 2025
I would have just used sprintf for the whole thing rather than mixing two different string creation methods, like this, with a single sprintf():
caption = sprintf("Plate: %s.\nGrain Size: %f\n\nT_o_Q: %.1f\260C (%.1f\260F)", ...
InputSheet, Grain{n}, T_oQlast, T_oQlast*1.8+32)
title(caption, ...
'FontWeight', 'normal', 'FontSize',18, 'FontName','Arial',...
'Units','normalized', 'Position', [0.02, 0.98], ...
'HorizontalAlignment', 'left', 'VerticalAlignment','top')

Accedi per commentare.

Più risposte (1)

dpb
dpb il 25 Lug 2025
Modificato: dpb il 25 Lug 2025
In
title(['Plate ' InputSheet;'Grain Size' string(Grain(n));...
the semicolon is trying to concatenate two char() strings vertically which cannot be done unless they are the same length; char() strings are just arrays of bytes and so like any other array, they must be conformant in dimensions in order to be able be able to be appended.
You don't want to do that anyway, what you want is a newline character in the title string...
title(['Plate ' InputSheet newline 'Grain Size' string(Grain(n));...
sprintf(['T_o_Q: %.1f' char(176) 'C (%.1f' char(176) 'F)'],T_oQlast (T_oQlast*1.8)+32);],...
'FontWeight','normal','FontSize',18,'FontName','Arial','Units','normalized',...
'Position',[0.02 0.98],'HorizontalAlignment','left','VerticalAlignment','top')
presuming that InputSheeet is also a char() string variable.
You may still run into issues; as @the cyclist notes, what are the variables may cause other errors to appear.
I'd probably recast the above slightly for legibility and to avoid mixing char() and string variables...
str1=sprintf(['Plate %S' newline 'Grain Size %s'],InputSheet,Grain(n));
str2=sprintf(['T_o_Q: %.1f' char(176) 'C (%.1f' char(176) 'F)'],T_oQlast,C2F(T_oQlast));
str=[str1 newline str2];
title(str,'FontWeight','normal', 'FontSize',18,'FontName','Arial', ...
'Units','normalized', 'Position',[0.02 0.98], ...
'HorizontalAlignment','left','VerticalAlignment','top')
Even that may yet have some typos to cleanup, but should be easier to debug to get the substrings formatted as desired.
  3 Commenti
dpb
dpb il 25 Lug 2025
Modificato: dpb il 25 Lug 2025
"Grain(n) is a 1x1 cell"
Then you need to dereference it with the curly braces instead of parentheses...
You didn't take the suggestion to refactor to make things easier to debug, though. That will help.
Factor into simpler pieces and use a breakpoint to inspect each -- then can fixup what typos there are much more simply when can see them.
I forget that F2C{} isn't built in (why not?) but a utility function in my Utilis folder...define
F2C=@(F)1.8*F+32;
Stephen23
Stephen23 il 25 Lug 2025
Modificato: Stephen23 il 25 Lug 2025
"the semicolon is trying to concatenate two char() strings vertically which cannot be done unless they are the same length; char() strings are just arrays of bytes and so like any other array, they must be conformant in dimensions in order to be able be able to be appended."
Due to the string within the concatenation an overloaded string concatenation is called, within which the character vectors are first converted to string scalars:
['hi';'this is a much longer character vector';"a string"]
ans = 3×1 string array
"hi" "this is a much longer character vector" "a string"
So we can see that the claimed reason for the error is not correct, it works without error.
The actual reason occurs due to the horizontal concatenation creating a 1x2 string array, which then cannot be vertically concatenated with the 1x1 strings:
['this is not the problem';'this row',"is the cause"]
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by