Issue with updating xlabel/ylabel

조회 수: 5 (최근 30일)
Paul
Paul 2014년 1월 20일
댓글: Paul 2014년 1월 21일
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
AJ von Alt
AJ von Alt 2014년 1월 20일
Can you attach your code and gui for review?
Paul
Paul 2014년 1월 21일
편집: Paul 2014년 1월 21일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

AJ von Alt
AJ von Alt 2014년 1월 21일
편집: AJ von Alt 2014년 1월 21일
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
Paul
Paul 2014년 1월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Doug Hull
Doug Hull 2014년 1월 20일
Can you save the handle of the updated label, and just change the string property?
  댓글 수: 3
Walter Roberson
Walter Roberson 2014년 1월 21일
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 2014년 1월 21일
편집: Paul 2014년 1월 21일
Okay, gave it a shot. Still same issue.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by