0

I am building a GUI using App Designer in MATLAB (2019b). One aspect of this is allowing the user to load a file, and then displaying the name of the file that was loaded. When the text to display is longer than the uilabel size, the default behavior is to truncate the end and append an ellipsis ("..."). I would like to truncate from the front and place an ellipsis there, as it's more important for the user to see the file name then the file path. Here's an example of what I want it to look like.

sample app image

What is the best way to achieve this? Right now I have a hack that measures the width of the uilabel and then approximates how many characters would fit and truncates the text based on that. This doesn't seem to behave consistently though, probably do to proportional spacing rather than monospacing. Also, since the width of the uilabel is measured in pixels, I worry that it won't behave consistently on other monitor resolutions and display environments. Is there a better way than what I'm currently doing, or a way to improve my method to make it more reliable?

Sample Code:

classdef sampleApp < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
    UIFigure               matlab.ui.Figure
    SelectaFileButton      matlab.ui.control.Button
    defaultFilename        matlab.ui.control.Label
    DefaultBehaviorLabel   matlab.ui.control.Label
    ModifiedBehaviorLabel  matlab.ui.control.Label
    modifiedFilename       matlab.ui.control.Label
end

% Callbacks that handle component events
methods (Access = private)

    % Button pushed function: SelectaFileButton
    function SelectaFileButtonPushed(app, event)
        [filename,pathname] = uigetfile('Select a filename to display');
        filestr = fullfile(pathname,filename);

        % This sets the text the normal way
        app.defaultFilename.Text = filestr;
        app.defaultFilename.Tooltip = filestr;

        % This measures the width of the text field and attempts to fit
        % the text to the field
        pos = app.modifiedFilename.Position;
        nchars = floor(0.165*pos(3))-3; % The 0.165 was found through trial and error
        if(length(filestr)>nchars+3)
            shortText = ['...' filestr(end-nchars+1:end)];
        else
            shortText = filestr;
        end   
        app.modifiedFilename.Text = shortText;
        app.modifiedFilename.Tooltip = filestr;
    end
end

% Component initialization
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % Create UIFigure and hide until all components are created
        app.UIFigure = uifigure('Visible', 'off');
        app.UIFigure.Position = [100 100 297 111];
        app.UIFigure.Name = 'UI Figure';

        % Create SelectaFileButton
        app.SelectaFileButton = uibutton(app.UIFigure, 'push');
        app.SelectaFileButton.ButtonPushedFcn = createCallbackFcn(app, @SelectaFileButtonPushed, true);
        app.SelectaFileButton.Position = [13 68 160 22];
        app.SelectaFileButton.Text = 'Select a File';

        % Create defaultFilename
        app.defaultFilename = uilabel(app.UIFigure);
        app.defaultFilename.Position = [118 39 133 22];
        app.defaultFilename.Text = 'Filename';

        % Create modifiedFilename
        app.modifiedFilename = uilabel(app.UIFigure);
        app.modifiedFilename.Position = [118 19 143 22];
        app.modifiedFilename.Text = 'Filename';

        % Create DefaultBehaviorLabel
        app.DefaultBehaviorLabel = uilabel(app.UIFigure);
        app.DefaultBehaviorLabel.HorizontalAlignment = 'right';
        app.DefaultBehaviorLabel.Position = [-2 40 110 22];
        app.DefaultBehaviorLabel.Text = 'Default Behavior:';

        % Create ModifiedBehaviorLabel
        app.ModifiedBehaviorLabel = uilabel(app.UIFigure);
        app.ModifiedBehaviorLabel.HorizontalAlignment = 'right';
        app.ModifiedBehaviorLabel.Position = [-2 20 110 22];
        app.ModifiedBehaviorLabel.Text = 'Modified Behavior:';

        % Show the figure after all components are created
        app.UIFigure.Visible = 'on';
    end
end

% App creation and deletion
methods (Access = public)

    % Construct app
    function app = sampleApp

        % Create UIFigure and components
        createComponents(app)

        % Register the app with App Designer
        registerApp(app, app.UIFigure)

        if nargout == 0
            clear app
        end
    end

    % Code that executes before app deletion
    function delete(app)

        % Delete UIFigure when app is deleted
        delete(app.UIFigure)
    end
end
end
David K
  • 1,296
  • 18
  • 39
  • What does the text box display if you choose `HorizontalAlignment = 'left'` (_instead of right_) ?? – Hoki Apr 07 '20 at 15:03
  • @Hoki It displays the same, it just changes which side the text is pinned to. The default is actually left, but I had tried right hoping it would change it. – David K Apr 07 '20 at 16:01

1 Answers1

0

The ellipses are probably added in the system APIs (Java), which may not necessarily be accessible to matlab. I suggest you to show the file name only in the label, and extract the path use fileparts and display it as TooltipString (supported in R2018) of the label.

FangQ
  • 1,444
  • 10
  • 18