# Is there a way to fit long text in a grid with ellipsis?

1 view (last 30 days)
Eddie on 26 Jan 2020
Commented: Eddie on 26 Jan 2020
Here is an example of what I mean -
The above images are arranged in a Grid layout (using GUI Layout Toolbox), so each axes are in their own uicontainer, which means any text from one grid will be overlaid on top by the a subsequent grid, therefore hiding the text if it's beyond the boundary of its own grid. As you can see, the texts (.170) and (.txt) are partially hidden because of this.
To solve this problem, one would have to replace the extra text with ellipsis (...) to shorten the text and make it fit into the grid. An example is the Windows explorer, showing below, where the text (.170) is replaced with (.1...) as it is shorter in length.
Apparently, this requires the dertermination of the width (or length) of the actual text, which has a lot to do with the font size and font used. Unless it's monospaced font, using number of characters to determine the length is not enough. For example, 'iii' is much shorter than 'DDD' in width.
In MATLAB, although you can determine the width of the graphics text object, the extent of the box that encloses the text is a read-only property, therefore it's not possible to restrict the width of the text object (and MATLAB also has to know to put ellipsis when the text is restrained). A lengthy workaround would be to calculate the width of the text based on its font and font size, and if it's wider than the grid, you'll have to iteratively shortern its characters one by one and determine whether the shortened text would fit inside the grid, while adding ellipsis into the mix.

Adam Danz on 26 Jan 2020
I don't have this toolbox so I can't play around with it but you could putting line breaks in to the text such as
s = ['Protein-DNA',newline,'complex.txt'];
% or
s = sprintf('Protein_DNA\ncomplex.txt');
I have no idea if the line break will be interpreted correctly or whether the 2nd line will be cut off.
Eddie on 26 Jan 2020
That won't work on very long file names, however.
A related problem, but in reverse, was asked in the following post before. In that post, the poster wanted to use multi-line instead of ellipsis (albeit that is on a checkbox uicontrol instead of a text axis object).