Adjust Position of text in a png file created using uicontrol
Mostrar comentarios más antiguos
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
el 8 de Nov. de 2024
Respuestas (1)
Voss
el 8 de Nov. de 2024
0 votos
- 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')
"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.
Categorías
Más información sobre Scripts 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!
