How to align matrices in a static text box?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
So, i am working on LU decomposition calculator, and other matrices aren't align. I have no idea how to do it.
4 comentarios
Geoff Hayes
el 27 de Feb. de 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.
Respuestas (1)
Adam Danz
el 27 de Feb. de 2020
Editada: Adam Danz
el 6 de Mzo. de 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.
0 comentarios
Ver también
Categorías
Más información sobre String en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
