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 件のコメント

the cyclist
the cyclist 2025 年 7 月 25 日
I'm not sure it is possible to diagnose the problem without the variable you are using to create the text. Can you upload the variables in a MAT file? You can use the paper clip icon in the INSERT section of the toolbar.
John
John 2025 年 7 月 25 日
Stephen23
Stephen23 2025 年 7 月 25 日
編集済み: Stephen23 2025 年 7 月 25 日
"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 2025 年 7 月 25 日
Thank you stephen23 this was confussing me!

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2025 年 7 月 25 日
編集済み: Star Strider 2025 年 7 月 25 日

0 投票

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 件のコメント

John
John 2025 年 7 月 25 日
編集済み: John 2025 年 7 月 25 日
For the code
%.1f%sC (%.1f%sF)
%.1f sets decimal limit to one degree,
%s is a placeholder for T_oQlast? A character or vector string
C or F is for degrees C or F
In the line
sprintf('T_o_Q: %.1f%sC (%.1f%sF)',T_oQlast, degsign (T_oQlast*1.8)+32, degsign)
degsign = string(char(176))
I'm confussed how matlab knows to say,
T_o_Q: -41.8°C (-43.2°F)
After 'T_o_Q: %.1f%sC (%.1f%sF)' there is , T_oQlast, degsign how does it know these two things go into the first part where %s is and not more?
Does my question make sense enough to answer, I lack the knowledge to ask this in an intelligent manor?
Stephen23
Stephen23 2025 年 7 月 25 日
編集済み: Stephen23 2025 年 7 月 25 日
Note that calling STRING is not required anywhere, you can write simpler code without it:
InputSheet = 'Plate #1';
Grain = {42};
n = 1;
T_oQlast = 84;
str = ["Plate "+InputSheet; "Grain Size "+Grain{n};...
sprintf("T_o_Q: %.1f\260C (%.1f\260F)",T_oQlast, T_oQlast*1.8+32)]
str = 3×1 string array
"Plate Plate #1" "Grain Size 42" "T_o_Q: 84.0°C (183.2°F)"
title(str,...
'FontWeight','normal','FontSize',18,'FontName','Arial','Units','normalized',...
'Position',[0.02 0.98],'HorizontalAlignment','left','VerticalAlignment','top')
"how does it know these two things go into the first part where %s is and not more?"
SPRINTF (and FPRINTF) keeps on applying the format to the elements of an input variable until it runs out of elements. Then it moves onto the next variable and repeats. Consdier this:
sprintf('%.1f%s',pi,'xxx')
ans = '3.1xxx'
it will iterate through all of the elements of the first variable. In this case, the first variable is scalar so it has one element. Thus %.1f is printed with the value of that one element. Then SPRINTF moves onto the next variable (because it has run out of elements from the 1st variable) and tries to print that. In this case the next variable is a character vector (or a scalar string, it is irrelevant which in this case) which also gets used up at once by the %s format. Done.
John
John 2025 年 7 月 25 日
Ah thats a great explaniation, thank you!
Star Strider
Star Strider 2025 年 7 月 25 日
Stephen, thank you.
Image Analyst
Image Analyst 2025 年 7 月 26 日
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')

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2025 年 7 月 25 日
編集済み: dpb 2025 年 7 月 25 日

0 投票

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 件のコメント

John
John 2025 年 7 月 25 日
InputSheet is a 1x6 char string.
Grain(n) is a 1x1 cell
T_oQLast is a 1x1 Double
This gives me a new error message.
Array indices must be positive integers or logical values.
Error in SEBPostTest (line 527)
sprintf(['T_o_Q: %.1f' char(176) 'C (%.1f' char(176) 'F)'],T_oQlast (T_oQlast*1.8)+32);],...
^^^^^^^^^^^^^^^^^^^^^^^
dpb
dpb 2025 年 7 月 25 日
編集済み: dpb 2025 年 7 月 25 日
"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 2025 年 7 月 25 日
編集済み: Stephen23 2025 年 7 月 25 日
"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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

製品

リリース

R2024b

タグ

質問済み:

2025 年 7 月 25 日

コメント済み:

2025 年 7 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by