问题描述
我在GUI中有一个静态文本,需要编写如下内容:
I have a static text in GUI and need to write something like:
y1 = A[1]
y2 = A[2]
y3 = A[3]
等(有一定的限制,现在这并不重要).
etc. (there is a certain limit, right now this is not important).
其中y1, y2, y3
是字符串,而A[1], A[2], A[3]
是数字在矩阵A
中.
Where y1, y2, y3
are strings, and A[1], A[2], A[3]
are numbers in a matrix A
.
我只是不确定如何在该静态文本中输入新行,这意味着我不确定如何从y1
转到y2
.
I'm just not sure how to enter a new line in that static text, meaning I'm not sure how to go from y1
to y2
.
我知道互联网上对此问题有很多答案,但是由于某种原因我找不到适合我需求的答案.
I know there are a lot of answers to this question on the Internet but I can't find one that suits my needs for some reason.
我该怎么做?
谢谢.
推荐答案
请确保static-text
的伸展距离足以容纳这些多行文本.这是一个演示,其中包含A
中的一些样本值.诀窍是在每个单元格代表每个这样的赋值文本时使用Nx1 cell array
.代码必须清楚-
Make sure that the static-text
is stretched far enough to accommodate those multi-lines text. Here's a demo with some sample values in A
. The trick is to use a Nx1 cell array
with each cell representing each such assignment text. The code must make it clear -
A = [4 9 22 29 34 47 56 78 100]; %// Assumed as a vector for demo
cellstrg = cell(numel(A),1);
for k = 1:numel(A)
cellstrg(k) = {['y',num2str(k),' = ' ,num2str(A(k))]};
end
set(handles.text1,'String',cellstrg) %// handles.text1 is tag to that static text
您可以使用cellfun
方法避免for循环,这种方法可能效率不高,但只是实现同一件事的一种简洁方法-
You can avoid the for-loops with a cellfun
approach, which might not be efficient, but just a concise way of achieving the same thing -
cellstrg = strcat('y',cellstr(num2str([1:numel(A)]')),'=',strtrim(cellstr(num2str(A'))))
在GUI上输出-
这篇关于在GUI的静态文本中创建多行“字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!