How to align matrices in a static text box?

4 views (last 30 days)
Lee Marc Caya
Lee Marc Caya on 26 Feb 2020
Edited: Adam Danz on 6 Mar 2020
So, i am working on LU decomposition calculator, and other matrices aren't align. I have no idea how to do it.
  4 Comments
Geoff Hayes
Geoff Hayes on 27 Feb 2020
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 on 27 Feb 2020
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.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 27 Feb 2020
Edited: Adam Danz on 6 Mar 2020
"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.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!