printmat function for unknown number of rows
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cedric
2013-4-15
编辑:Cedric
2013-4-15
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 个评论
Cedric
2013-4-15
编辑:Cedric
2013-4-15
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 ;-)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!