printmat function for unknown number of rows
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Example:
M= rand(5)
printmat(M, 'My Matrix', 'ROW1 ROW2 ROW3 ROW4 ROW5', 'FOO BAR BAZ BUZZ FUZZ' )
but suppose I don't know how many rows I will have (it depends on the user input) what can I do in this situation?
Thank's.
0 comentarios
Respuesta aceptada
Cedric
el 15 de Abr. de 2013
Editada: Cedric
el 15 de Abr. de 2013
You can build your headers using SPRINTF; for example:
>> M = rand(6) ;
>> vheader = sprintf('ROW%d ', 1:size(M,1))
vheader =
ROW1 ROW2 ROW3 ROW4 ROW5 ROW6
(you might want to STRTRIM the output to get rid of the last white space)
2 comentarios
Cedric
el 15 de Abr. de 2013
Editada: Cedric
el 15 de Abr. de 2013
Well, in your example, you have the following call:
printmat(M, 'My Matrix', 'ROW1 ROW2 ROW3 ROW4 ROW5', 'FOO BAR BAZ BUZZ FUZZ')
where M is a numeric array, and the next 3 arguments are strings. I assume that 'ROW1 ROW2 ROW3 ROW4 ROW5' is the string defining the vertical header in the output, and you could actually have it saved in a variable that you pass to this function printmat of yours:
vheader = 'ROW1 ROW2 ROW3 ROW4 ROW5' ;
printmat(M, 'My Matrix', vheader, 'FOO BAR BAZ BUZZ FUZZ')
and more generally:
label = 'My Matrix'
vheader = 'ROW1 ROW2 ROW3 ROW4 ROW5' ;
hheader = 'FOO BAR BAZ BUZZ FUZZ'
printmat(M, label, vheader, hheader) ;
Now the example that I gave shows you how to build vheader if you don't know the number of rows of M in advance:
label = 'My Matrix'
vheader = sprintf('ROW%d ', 1:size(M,1)) ;
hheader = 'FOO BAR BAZ BUZZ FUZZ'
printmat(M, label, vheader, hheader) ;
Now it's up to you to adapt this to your homework ;-)
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots 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!