Issue with updating xlabel/ylabel

10 views (last 30 days)
Paul
Paul on 20 Jan 2014
Commented: Paul on 21 Jan 2014
Hi all. I have what may be a somewhat odd question. I have a GUI that I use for data analysis. In that GUI is a uipanel with a set of axes automatically created (actually, there are multiple panels and axes, only one is visible/active at a time). I also have in the axes a context menu that allows the user to apply scaling/offset parameters to the plotted data (i.e. you can offset all the x-axis data by 100 and it will add 100 to the xdata array of all the lines, etc). This all works fine.
When a user uses either of these context menu options, I also want to update the xlabel and ylabel to reflect the change. I have done this by a function that updates the xlabel or ylabel on demand doing something like this:
xtitle = get(get(curr_axes,'xlabel'),'String'));
xoffset = 'Some String from inputdlg';
delete(get(curr_axes),'xlabel'));
xlabel(curr_axes,[xtitle,' + [',xoffset,']']);
Again, this actually works fine as well - everything is updated and looks great. Everything is on one line and oriented properly. It looks something like this, all on one line:
Original X Title + [offset]
The issue comes when I try to update an xlabel or ylabel that has already been updated. For example, I also have a context menu item that allows the user to manually change the xlabel and ylabel strings. If the user does this, when they go to apply an offset or scalar, the (x/y)label becomes a fixed width and some weird word wrap takes place. It then looks something like this:
X Title
+ [
xoffset
]
And now the xlabel takes up four lines instead of one, and there's only a few characters on each line.
The code I'm using to update the label manually (not append it) is basically the same as the above code, except I just pass a normal string to the xlabel command instead of a concatenated string. I also tried deleting the old one to see if that was causing any issues with parameters being held over (like 'OuterPosition' or 'Units,' etc), but that does not seem to have resolved it either.
Any ideas?
  2 Comments
AJ von Alt
AJ von Alt on 20 Jan 2014
Can you attach your code and gui for review?
Paul
Paul on 21 Jan 2014
Edited: Paul on 21 Jan 2014
Unfortunately no. Not only is it about 20,000 lines spread across multiple folders, classes, and functions, but it also contains a lot of proprietary and confidential code. I could give more code around the work I'm trying to do here, but I think this is the relevant code.

Sign in to comment.

Accepted Answer

AJ von Alt
AJ von Alt on 21 Jan 2014
Edited: AJ von Alt on 21 Jan 2014
Check if the xlabel string is a cell array when you retrieve it.
If it is a cell, then the code:
xoffset = blah;
xtitle = get( get( gca , 'xlabel' ) , 'string' );
stringToWrite = [xtitle,' + [',xoffset,']']
set( get(gca , 'xlabel') ,'string' ,stringToWrite )
will add xtitle , ' + [' , xoffset and ']' as elements of the cell array stringToWrite. When the 'string' property of xlabel is set to a cell array, each element of the cell array is printed on a separate line. This would explain why your labels appear to be wrapping.
  1 Comment
Paul
Paul on 21 Jan 2014
Wow, I feel like an idiot. It was so obvious.
For anybody that cares, my issue was that I was using inputdlg to get the new label string for the function that allows the user to manually set the label, and that returns a string in a cell, and I wasn't stripping it out of the cell when I assigned it to the label. So then when I tried to concatenate in the other function, it was doing exactly as AJ von Alt said and making a cell array of strings before passing it back to the label.

Sign in to comment.

More Answers (1)

Doug Hull
Doug Hull on 20 Jan 2014
Can you save the handle of the updated label, and just change the string property?
  3 Comments
Walter Roberson
Walter Roberson on 21 Jan 2014
The xlabel() and ylabel() calls set the axis XLabel and YLabel properties to the handles of text objects. So
set( get(gca,'XLabel'), 'String', 'Some New String')
Paul
Paul on 21 Jan 2014
Edited: Paul on 21 Jan 2014
Okay, gave it a shot. Still same issue.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!