Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
text alignment in GUI for long string

Subject: text alignment in GUI for long string

From: craq

Date: 6 Aug, 2012 14:27:14

Message: 1 of 13

Hi,

I have a GUI with limited space, and a long string I want to put in it. The end of the string is more interesting (to be specific, it's a filename) so I would like to have the text right-aligned and clip the beginning. Does anybody out there know how to do this?

The following example demonstrates the problem:
fh = figure;
uicontrol(fh,'style','edit','string','abcdefjh/ijklmnop/qrstuvw.xyz', 'tag', 'base', ...
'HorizontalAlignment','right',...
    'Units','normalized', 'Position',[0.21 0.94 0.18 0.04]);

get(findobj(fh,'tag','base'),'HorizontalAlignment')

Apparently a bug which was something like the opposite of this was fixed in 2006
http://www.mathworks.de/support/solutions/en/data/1-13R8BC/index.html?product=ML&solution=1-13R8BC

thanks in advance

Subject: text alignment in GUI for long string

From: matt dash

Date: 7 Aug, 2012 03:57:08

Message: 2 of 13

"craq" wrote in message <jvok82$4p0$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have a GUI with limited space, and a long string I want to put in it. The end of the string is more interesting (to be specific, it's a filename) so I would like to have the text right-aligned and clip the beginning. Does anybody out there know how to do this?
>
> The following example demonstrates the problem:
> fh = figure;
> uicontrol(fh,'style','edit','string','abcdefjh/ijklmnop/qrstuvw.xyz', 'tag', 'base', ...
> 'HorizontalAlignment','right',...
> 'Units','normalized', 'Position',[0.21 0.94 0.18 0.04]);
>
> get(findobj(fh,'tag','base'),'HorizontalAlignment')
>
> Apparently a bug which was something like the opposite of this was fixed in 2006
> http://www.mathworks.de/support/solutions/en/data/1-13R8BC/index.html?product=ML&solution=1-13R8BC
>
> thanks in advance

Horizontal alignment doesnt matter... this is another job for findjobj:
http://www.mathworks.com/matlabcentral/fileexchange/14317

fh = figure;

u=uicontrol(fh,'style','edit','string','abcdefjh/ijklmnop/qrstuvw.xyz', 'tag', 'base', ...
'HorizontalAlignment','right',...
    'Units','normalized', 'Position',[0.21 0.94 0.18 0.04]);

j=findjobj(u)
j.setCaretPosition(length(get(u,'string')))

Subject: text alignment in GUI for long string

From: craq

Date: 7 Aug, 2012 07:19:23

Message: 3 of 13

thanks Matt (and Yair, I've picked up a fair bit on UI programming from your blog)

that works great... although in an ideal world I would've preferred not to add an external non-standard dependency.

Subject: text alignment in GUI for long string

From: craq

Date: 7 Aug, 2012 09:49:15

Message: 4 of 13

one small thing... the setting is not persistent. I have it set in my callback function, so that every time I change the string, it updates the CaretPosition. But this doesn't work if I click in the text field, and then somewhere else on the GUI. The callback is not triggered, and the text goes back to normal (left aligned). Is there a workaround for this?

Subject: text alignment in GUI for long string

From: matt dash

Date: 7 Aug, 2012 15:01:13

Message: 5 of 13

"craq" wrote in message <jvqoar$eei$1@newscl01ah.mathworks.com>...
> one small thing... the setting is not persistent. I have it set in my callback function, so that every time I change the string, it updates the CaretPosition. But this doesn't work if I click in the text field, and then somewhere else on the GUI. The callback is not triggered, and the text goes back to normal (left aligned). Is there a workaround for this?

Here's a slight modification:

function edit_test
fh = figure;
u=uicontrol(fh,'style','edit','string','abcdefjh/ijklmnop/qrstuvw.xyz', 'tag', 'base', ...
'HorizontalAlignment','right',...
    'Units','normalized', 'Position',[0.21 0.94 0.18 0.04]);
get(findobj(fh,'tag','base'),'HorizontalAlignment');

j=findjobj(u);
j.setCaretPosition(length(get(u,'string')))
set(j,'FocusLostCallback',{@scrollright,u})

function scrollright(j,ev,hObject)
j.setCaretPosition(length(get(hObject,'string')));

Subject: text alignment in GUI for long string

From: craq

Date: 7 Aug, 2012 15:33:13

Message: 6 of 13

brilliant! thanks =)

Subject: text alignment in GUI for long string

From: Yair Altman

Date: 7 Aug, 2012 15:39:17

Message: 7 of 13

related articles:

http://UndocumentedMatlab.com/blog/setting-line-position-in-edit-box-uicontrol
http://UndocumentedMatlab.com/blog/rich-matlab-editbox-contents
http://UndocumentedMatlab.com/blog/customizing-listbox-editbox-scrollbars

Yair Altman
http://UndocumentedMatlab.com
 

Subject: text alignment in GUI for long string

From: Milan

Date: 21 Sep, 2012 06:15:15

Message: 8 of 13

May it be possible, that the setCaretPosition attribute was introduces after 2010b? Matlab cries loud that this attribute cannot be assigned. I did not create the object by using uicontrol, thou. I used GUIDE to create a text box and I manipulated that with the suggested script. M

"matt dash" wrote in message <jvrajp$oqg$1@newscl01ah.mathworks.com>...
[snip]
> j.setCaretPosition(length(get(u,'string')))
[snip]

Subject: text alignment in GUI for long string

From: craq

Date: 21 Sep, 2012 07:09:10

Message: 9 of 13

I don't know when it was introduced, but I'm using 2010a and it works here. Have you tried running the test function that Matt posted?

Subject: text alignment in GUI for long string

From: Yair Altman

Date: 21 Sep, 2012 09:43:09

Message: 10 of 13

"Milan" wrote in message <k3h0li$7ua$1@newscl01ah.mathworks.com>...
> May it be possible, that the setCaretPosition attribute was introduces after 2010b? Matlab cries loud that this attribute cannot be assigned. I did not create the object by using uicontrol, thou. I used GUIDE to create a text box and I manipulated that with the suggested script. M
>
> "matt dash" wrote in message <jvrajp$oqg$1@newscl01ah.mathworks.com>...
> [snip]
> > j.setCaretPosition(length(get(u,'string')))
> [snip]


There's a difference between single-line and multi-line editbox uicontrols - they are actually two separate controls, and you may have used the multi-line control which behaves somewhat differently - read my links earlier in this thread to learn more.

Alternately, it is possible that findjobj did not find the underlying java control of your editbox for any of several possible reasons - in this case j would be [] rather than a Java object reference handle.

Yair Altman
http://UndocumentedMatlab.com
Read my Matlab-Java programming book
 

Subject: text alignment in GUI for long string

From: Milan

Date: 21 Sep, 2012 11:24:08

Message: 11 of 13

With a script from Matt, I've got an error one line earlier:

Error in ==> edit_test at 8
j=findjobj(u);
 
??? Undefined function or method 'findjobj' for input arguments of type 'double'.

"craq" wrote in message <k3h3qm$hod$1@newscl01ah.mathworks.com>...
> I don't know when it was introduced, but I'm using 2010a and it works here. Have you tried running the test function that Matt posted?

Subject: text alignment in GUI for long string

From: Milan

Date: 21 Sep, 2012 11:35:08

Message: 12 of 13

Thank you Yiar, thank you craq,

I knowledge that you put effort in helping me, but the recipe exceeds my knowledge and time I can invest in the task. I cut my long strings and displayed only the important parts at the ends. The minor inconvenience is, that now, when changing the GUI and the text boxes, I have to recalculate (experimentally!) the worthy length in the script itself.

A horizontal alignment to the right is horizontal alignment to the right, and nothing else. If MATLAB ignores this definition for, let us say, long strings, I consider it a bug.

I wish you a nice weekend, M

Subject: text alignment in GUI for long string

From: Yair Altman

Date: 21 Sep, 2012 11:39:08

Message: 13 of 13

"Milan" wrote in message <k3hioo$6av$1@newscl01ah.mathworks.com>...
> With a script from Matt, I've got an error one line earlier:
>
> Error in ==> edit_test at 8
> j=findjobj(u);
>
> ??? Undefined function or method 'findjobj' for input arguments of type 'double'.
>
> "craq" wrote in message <k3h3qm$hod$1@newscl01ah.mathworks.com>...
> > I don't know when it was introduced, but I'm using 2010a and it works here. Have you tried running the test function that Matt posted?


Well then, obviously you need the findjobj function, don't you?
Google gives you the answer far faster than it took you to write this post...
In case you have difficulty: http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj

Yair

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us