Print first few lines of output

27 vues (au cours des 30 derniers jours)
alpedhuez
alpedhuez le 23 Jan 2021
Commenté : Walter Roberson le 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Jan 2021
Modifié(e) : Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 24 Jan 2021
T = table(randn(20,1), rand(20,1)*5-2);
HEAD('summary(T)', 8)
Variables: Var1: 20x1 double Values: Min -1.6334
HEAD('T+1', 4)
Error using evalin Undefined function 'plus' for input arguments of type 'table'. Error in LiveEditorEvaluationHelperEeditorId>HEAD (line 10)
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
Walter Roberson le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by