Print first few lines of output
24 views (last 30 days)
Show older comments
alpedhuez
on 23 Jan 2021
Commented: Walter Roberson
on 24 Jan 2021
Suppose I do
summary(T)
Then it will print out statistics of all the variables in the table, which is good. But at the same time it will output all the information and will create "auto-scroll" that is very cumbersome. Thus I want to print out the first five lines of the output.
head(summary(T),5)
does not work.
0 Comments
Accepted Answer
Walter Roberson
on 23 Jan 2021
Edited: Walter Roberson
on 23 Jan 2021
summary(T(:,1))
If summary() was just being used as an example, then
temp = regexp(evalc("Put The Command Here"), '\n', 'split');
fprintf('%s\n', temp{1:min(end,5)});
such as
temp = regexp(evalc("summary(T)"), '\n', 'split');
fprintf('%s\n', temp{1:min(end,5)});
You can build a function to do this for you.
5 Comments
Walter Roberson
on 24 Jan 2021
T = table(randn(20,1), rand(20,1)*5-2);
HEAD('summary(T)', 8)
HEAD('T+1', 4)
function details___ = HEAD(COMMAND__, N__)
if nargin < 2; N__ = 5; end
try
CMD__ = "evalin('caller', '" + COMMAND__ + "')";
details__ = evalc(CMD__);
catch ME__
details__ = getReport(ME__);
end
detail_lines = regexp(details__, '\n', 'split');
details__ = strjoin(detail_lines(1:min(N__,end)), '\n');
if nargout == 0
disp(details__);
else
details___ = details__;
end
end
Walter Roberson
on 24 Jan 2021
Note that the code was designed so that if an error message is the output of the command, then only the first N lines of the error message are output.
The code is designed so that you can assign the output of HEAD() to a variable, but that if you do not do so then it outputs to the display.
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!