Search code examples
matlabtextmatlab-guideword-wrap

Prevent word wrap in GUI text


I have a MATLAB GUI that allows a user to load a configuration file. I then want the filename to be displayed in a static text field. My problem is that the string is too long for my text field and wraps around. I want the text to display as much of the string as it can without wrapping, prioritizing the end of the string.

For example, if I have a filename 'C:\folders\more\folders\thisismylongfilename.txt', I am currently seeing

C:\folders\more\folders\thisism
              ylongfilename.txt

If I use an edit text rather than a static text, I see C:\folders\more\folders\thisism

I would like my text field to display olders\thisismylongfilename.txt, or maybe ...ers\thisismylongfilename.txt. The missing portion can either be "displayed" but outside the visible box, or something I can remove before displaying. I would just need to know how much of the string to remove.

How can I properly display my long string in a fixed width text box?


Solution

  • One way to accomplish this would be read the length of your textbox and shorten the string before you display it.

    myString = 'path/to/file/file.txt';
    
    set(handles.textbox,'Units', 'Characters'); %set units to characters for convenience
    pos = get(handles.textbox,'Position'); %get the position info
    maxLength = floor(pos(3)); %extract the length of the box 
    if length(myString) > maxLength % cut beginning if string is too long 
        newStart = length(myString) - maxLength + 1;
        displayString = myString(newStart:end);
    else
        displayString = myString;
    end
    
    set(handles.textbox,'String', displayString);