How to switch between multiline edit text box to single line edit text box?

4 views (last 30 days)
The gui (created via guide) has two edit text boxes. Through edit text box 1, user supplies the number of entries to be made in edit text box 2. If number of entries to be made in edit text box 2 is greater than one, the edit text box 2 is transformed to a multi-line editable text box. The action is achieved through the following code in callback of edittextbox1:
set(handles.edittextbox2,'Max',str2double(get(handles.edittextbox1,'String')));
Now if the user again supplies the number of entries to be made to edit text box 2 to be one (in edit text box 1), the edit text box 2 has to become single line edit text box. But the edit text box 2 becomes invisible. How to switch back and forth between multiline edit text box and single line edit text box?

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jul 2017
If the String property of a uicontrol style text String property has been set to a cell array of characters with more than one entry, or if the String property has been set to a character vector that includes one or more newline characters, or if the String property has been set to a character array with more than one row, then when you set the Max property to 1, you will see an error message similar to
Warning: Single line Edit Controls can not have multi-line text
and the uicontrol will disappear.
The only workaround is "Don't do that!" . Or switch to Java components.
new_numlines = str2double(get(handles.edittextbox1,'String'));
current_content = cellstr(get(handles.edittextbox2, 'String'));
curlen = length(current_content);
if curlen < new_numlines
current_content{new_numlines} = ''; %other entries will fill in [] automatically
else
current_content(new_numlines+1:end) = []; %delete any excess
end
set(handles.edittextbox2, 'Max', new_numlines, 'String', current_content);
  2 Comments
Thanigaivel Raja T
Thanigaivel Raja T on 16 Jul 2017
Edited: Thanigaivel Raja T on 16 Jul 2017
Thank you very much Walter. The workaround works well. By the code, the editbox2 values is in cell array. Cell2mat and str2double had to be used to access the editbox2 values.
Walter Roberson
Walter Roberson on 16 Jul 2017
str2double() works on cell arrays of character vectors.
char() works to convert a cell array of character vectors to a character array with a consistent number of columns and blank padded at the end of rows as necessary. However, str2double() does not work on such arrays.
You needed to be concerned with cell arrays of character vectors anyhow: when you set a String property of a multiline edit control to include a newline, MATLAB automatically converts to a cell array of character vectors. There are also circumstances where a multi-row char array can get automatically converted to a cell array of character vectors as.
Basically, if you specifically set the String property to a multi-row char array and you never change the value, then you can be relatively sure that you will get out the same multi-row char array when you retrieve the property, but otherwise you need to expect that you might get back a cell array of character vectors even if you set the String property to a character vector or a multi-row char array.

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!