How do I enter and evaluate multiple lines in a GUI edit box that I created in MATLAB?

29 vues (au cours des 30 derniers jours)
I am trying to create a GUI with an edit box. In the edit box, I would like to be able to enter several lines and then evaluate the lines that I entered. Every time I press enter, the callback is evaluated without letting me add more lines.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 12 Août 2020
Modifié(e) : MathWorks Support Team le 10 Juil 2020
You can set a multi-line edit box by modifying the "max" uicontrol property of the edit box. If
MAX - MIN > 1
then editable text boxes accept multiline input. If
MAX - MIN <= 1
then editable text boxes accept only single line input.
You can change this parameter in two ways:
1) Double click on the edit box in question to bring up the Property Inspector.
2) Change the value of MAX to the desired number.
From the command line (or in a script):
1) Use the SET command with the following syntax:
where h_edit is the handle for the edit box and N is the desired number for MAX.
For more information on UICONTROL properties please take a look at the following documentation:
Please Note: The way the callback is executed is different for single and multiple lines. For more information on this, please see CALLBACK property from the above link.
You can evaluate the lines of text entered into the edit box in two ways. You can use the EVALMCW command (Evaluate Matlab Command Window) as follows:
where "h_edit" is the handle to the edit text box. The second method is to write your own FOR loop as follows:
s = get(h_edit,'string'); %h_edit is the handle to the edit box
[row, column] = size(s);
for i = 1:row
eval(s{i,:}) %evaluate each line as in MATLAB command prompt
  1 commentaire
raym le 2 Mar 2023
The s is a char matrix instead of cell.
Thus to get a multi-line text:
aCharmat = get(s{1},'string');
aCode = strjoin(arrayfun(@(x) strtrim(aCharmat(x,:)),1:size(aCharmat,1),'uni',0),sprintf('\n'))

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by