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

30 次查看(过去 30 天)
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.

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2020-7-10
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 个评论
raym
raym 2023-3-2
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'))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by