问题
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).
Where y1, y2, y3
are strings, and A[1], A[2], A[3]
are numbers in a matrix A
.
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.
How can I do that?
Thank you.
回答1:
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
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'))))
Output on GUI -

来源:https://stackoverflow.com/questions/23759822/creating-multi-line-string-in-a-static-text-of-a-gui