Title issues when plotting
Mostrar comentarios más antiguos
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 comentarios
the cyclist
el 25 de Jul. de 2025
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
el 25 de Jul. de 2025
"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.
John
el 25 de Jul. de 2025
Respuesta aceptada
Más respuestas (1)
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 comentarios
John
el 25 de Jul. de 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;
"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"]
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

