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

22 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 12 de Ag. de 2020
Editada: MathWorks Support Team el 10 de Jul. de 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:
From GUIDE:
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:
set(h_edit,'Max',N);
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:
evalmcw(h_edit)
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
end
  1 comentario
raym
raym el 2 de Mzo. de 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'))

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by