Put a % sign inside of an 'edit text' box behind a number in the box

Hi,
So I have a few edit text boxes on my form that I made using GUIDE. My text boxes are linked to some scroll bars and range from 1 to 100. These edit boxes are being used as 'weights'. The 1 to 100 is suppose to represent a percentage and I think the GUI would make more sense to the users if there was an actual % sign behind each number in the text boxes. I was wondering how I would accomplish this.
Basically within each text box say the number is 75... I want it to read "75%"
Thanks

 채택된 답변

Jan
Jan 2013년 7월 9일
편집: Jan 2013년 7월 9일
What about reading the inital number in the callback and appending a % afterwards. Then it does not matter if the user types "75" or "75%", or even "75 hello again, how are you?!":
uicontrol('Style', 'edit', 'String', '75 %', ...
'Callback', @addPercentCB);
...
function addPercentCB(ObjectH, EventData)
str = get(ObjectH, 'String');
num = sscanf(str, '%g', 1);
if length(num) ~= 1
warning('Bad number, using default instead');
num = 75;
end
str = sprintf('%.0f %%', num);
set(ObjectH, 'String', str);

댓글 수: 3

Thanks Jan,
One question though. It works great when someone types texts and presses enter or tab. It also works great for when the function is first created. But where it is not working is when the slider is used to change the value of the edit box. How can I fix this?
Set the corresponding code in the callback of the slider. Then you do not set the String property of "ObejctH" (which would be the slider itself) but you can obtain the edit field's handle from the handles struct.
Got it, thanks!

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

추가 답변 (1개)

Guru
Guru 2013년 7월 9일

0 개 추천

Create a static text box where you put in the % for the string value and position it immediately to the right of the edit text boxes. If you like, you can also change the BackgroundColor for the static textbox to be the same background color as the Edit Textbox which I think defaults to white.

댓글 수: 1

Yeah, I was really hoping there was a bit more programmatic way of doing it than the one you are describing. I would use this method as a last resort.

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

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2013년 7월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by