Need to position cursor at a specific character in a UIEditField string
Show older comments
I'm using a UIEditField in App Designer. I replace certain characters with symbols as the User types using a ValueChanging callback, see code below.
% Value changing function: Entry11, Entry11_2, Entry12, Entry12_2,
% ...and 2 other components
function GroupEntryValueChanging(app, event)
h = gcbo; % get handle of callback function
firstStr = strrep(event.Value,'D',char(176)); % change "d" to degree symbol
lastStr = strrep(firstStr,'A',char(8736)); % change "a" to angle symbol
h.Value = lastStr; % change edit box text
end
This works as desired and replaces the characters. The problem is cursor placement when inserting the special characters "A" or "D" in the middle of a string. The cursor jumps to the end of the string, which is annoying and not User friendly. And I understand the cursor will obviously be located at the end of the new string sent to the editbox.
I could correct this behavior using (1) strfind to know where the character is located in the old string and (2) position the cursor there after sending the new string. Is there any Matlab command or other code to position the cursor within the string? Perhaps this is the wrong approach, Is there an easier way to do this?
Kind regards,
Accepted Answer
More Answers (0)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!