Adjust Position of text in a png file created using uicontrol

23 visualizaciones (últimos 30 días)
Govind
Govind el 8 de Nov. de 2024 a las 10:45
Respondida: Voss el 8 de Nov. de 2024 a las 16:43
I have an app which generates a png with plot and some text at the bottom. The plots are fine, but the text at the bottom the position is a problem . So i did this using uicontrol. One line of code in the app is
txt_title1 = uicontrol('Style', 'text', 'String',app.saveText1,'FontSize',10,'Position', pos1);
where app.saveText1 is the string pos1 is the position where it needs to be in the png. Right now, I made the mistake of hardcoding it. So how the program works is it creates a figure in the background without opening it to the user and saves the figure.
newFigure = figure ('visible','off');
......
saveas(newFigure,[FileName,'.png']);
close(newFigure);
This is how I did it. But to find the position of text what I do is remove the visible off and close figure, so that app opens displays the new figure. Then I click on edit plot, then use property inspector, then drag and adjust the text to how I want it, find the position in property inspector and use that position as pos1 hardcoded in the program and then bring back visible off and close figure and generate the program for the user. And when user runs the program and click save, a figure will be created in background and will be saved in the folder. And that figure will have the text in the correct position. But unfortunately the position of the string app.saveText1 is not fixed. It changes and when it changes the position in the png will also change. I have uploaded two pics below to show the problem, one with the corrected by me dragging and adjusting and second the wrong one.
You can see the difference in both. So from the property instructor i understood the position is [left bottom width height]. So I thought i can use string length to find the length of the string and use that for width for position. But apparently they are both different. For example in the picture above you can see Battery Data Path. The string length of that is 95 whereas in the position of that in property inspector when i corrected its 859. So instead of hard coding, I need to find a way for app to work this automatically using the string length and then work accordingly. How can I do that. Kindly do help me. Thank you.
  1 comentario
Govind
Govind el 8 de Nov. de 2024 a las 12:53
Ok from further research I understood the Position in uicontrol has a default unit of pixels and we can change it to characters. But even after changing it to characters it is not correct, i adjusted the selection to the most minimum to display all variables like this.
The stringlength of this text is 95 characters. But here as you can see in property inspector it is 118.6. I also tried with text Hello. Hello, string length is 5 and the position width in characters is shown as 6.2. I simply tried use 5 as hard coded in position and then i am only getting hell in the figure. So what is this remaining 1.2, or the remaining 23.6 in batteryPath. I also tried Hello how are you, where the string length is 17 and in position it is shown as 22.3 which is 5.3 more. What is this extra value coming and is there a way for me to calculate that? Thank you.

Iniciar sesión para comentar.

Respuestas (1)

Voss
Voss el 8 de Nov. de 2024 a las 16:43
As explained in the documentation for uicontrol properties, when Units is 'characters',
  • Character width = width of the letter x.
That your string is not all 'x's is why the number of characters in the string is substantially different from the width stored in 'Position'.
You could change to a fixed-width font (i.e., one where all characters are the same width), e.g., uicontrol(...,'FontName','FixedWidth')
And/or you can use the 'Extent' property, as explained in its documentation:
"To adjust the width and height to accommodate the size of the String value, set the Position width and height values to be slightly larger than the Extent width and height values."
Note that you'll likely need a drawnow after setting the String/creating the uicontrol before setting the Position based on the Extent so that the value of the Extent property is up-to-date and accurate for the current String.
A completely different alternative is to use text objects instead of uicontrols, with 'Units' 'normalized' (i.e., the text is positioned relative to the axes it's contained in). That the texts are beneath the axes requires using a negative y-coordinate for their position but otherwise no special considerations, since the text 'Clipping' is 'off' by default, as stated in the text documentation.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by