How to align matrices in a static text box?

2 ビュー (過去 30 日間)
Lee Marc Caya
Lee Marc Caya 2020 年 2 月 26 日
編集済み: Adam Danz 2020 年 3 月 6 日
So, i am working on LU decomposition calculator, and other matrices aren't align. I have no idea how to do it.
  4 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 2 月 27 日
Lee - are you putting in the spaces between each number? Is that one static text fields with three lines or three different static text fields? Please provide your steps and or code that explains how you are entering in this matrix.
Lee Marc Caya
Lee Marc Caya 2020 年 2 月 27 日
lud = (get(handles.input1, 'String'));
s = str2num(lud);
[L,U,P] = lu(s);
t=num2str([U]);
set(handles.input2, 'string', [t]);
This is the code i use.
Question no. 1: i have no idea what that means
Question no. 2: One static text field with three lines.

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2020 年 2 月 27 日
編集済み: Adam Danz 2020 年 3 月 6 日
"Question no. 1: i have no idea what that means"
See the inline comments below for an explanation of each line.
% This retrieves the text (aka string) from the input1 handle
lud = (get(handles.input1, 'String'));
% This converts the string to numeric (ie '10' --> 10)
s = str2num(lud);
% I don't know what lu or s are so I can't help you here.
[L,U,P] = lu(s);
% The converts a number to a string (ie 10 --> '10')
t=num2str([U]);
% This sets the string back into the text box
set(handles.input2, 'string', [t]);
Question no. 2: One static text field with three lines.
Here's a demo that shows how vertically align columns of a matrix within a text box.
Pay close attention to the inline comments.
% Create a figure & a text box for the demo
fh = figure();
tbh = uicontrol(fh, 'Style','edit','Units','Normalize',...
'Position',[.1 .1 .5 .2],'Max',2,'FontName', 'Consolas');
% Note that the text box uses 'Consolas' as a font. It's important
% to chose a fixed-width font if you want vertical alignment.
% Create a numeric matrix
value = [randi(15,1,4); rand(2,4).*100];
% Convert the matrix to a cell of character arrays
vc = sprintfc('%g',value); %undocumented
% Count the number of characters in each character array
nchar = cellfun(@numel, vc);
% Pad the char arrays with empty spaces so they are all the same length
vcPad = cellfun(@(c,n){[c,repmat(' ',1,n)]},vc,num2cell(max(nchar(:))-nchar));
% Convert the cell array of chars to a char array
rowsJoined = arrayfun(@(row){strjoin(vcPad(row,:),' ')}, 1:size(vcPad,1));
charPad = vertcat(rowsJoined{:});
% Load the char array back into the text box
% NOTE: If your text box isn't wide enough, the rows will wrap and
% you'll lose the vertical alignment
tbh.String = (charPad);
Note that the text box must be wide enough to fit the text or it will wrap the text and you'll lose the vertical alignment.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by