Title issues when plotting
조회 수: 4 (최근 30일)
이전 댓글 표시
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
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"]
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"]
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.
채택된 답변
Star Strider
2025년 7월 25일
편집: Star Strider
2025년 7월 25일
Create everything as a string array --
InputSheet = "Plate #1";
Grain{1} = 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
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
2025년 7월 25일
편집: dpb
2025년 7월 25일
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
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
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"]
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"]
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

