How can I capture the lines written to the CLI by the disp() function?

3 views (last 30 days)
I have a table that I would like to save to a text file. The table.write method lets me do this, but only as delimited text. I would like to write the same lines that the disp() function writes to the command line when I call it with a table as input. Is there a way to capture those lines, either as a char array or a cell array?
  1 Comment
Bruce Elliott
Bruce Elliott on 7 Jun 2018
Edited: Bruce Elliott on 7 Jun 2018
It might help if I restate my question. What I am asking for is a way to produce the same result programatically that I can get by doing the following manually:
  1. Call disp(myTable) to display the table in the command line interface.
  2. Select the displayed lines and copy them to the system clipboard.
  3. Paste the copied lines to my text file.
Here is an example of what this looks like at the command line:
>> T = table([1:5]',[10:10:50]','VariableNames',{'Var_A','Var_B'});
>> disp(T)
Var_A Var_B
_____ _____
1 10
2 20
3 30
4 40
5 50
I can simply select the lines that display the table contents and copy them to my file to get exactly what I want.
Walter Roberson suggested in his answer that I try using evalc('disp(T)'), which gives me something close to what I want, but it includes markup code (for boldface), as below:
<strong>Var_A</strong><strong> Var_B</strong>
<strong>_____</strong> <strong>_____</strong>
1 10
2 20
3 30
4 40
5 50
I hope that clears up what I'm asking.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Jun 2018
result = evalc('disp(NameOfTableGoesHere)');
fid = fopen('NameOfOutputFile.txt', 'wt');
fwrite(fid, result);
fclose(fid);
result will already have embedded \n characters, which is why we do not format it and just send it to the file.
  8 Comments
Bruce Elliott
Bruce Elliott on 7 Jun 2018
Yes, except that on Windows it's "@table\'. I assume that the '@tabular/' is a Mac thing (?).
Walter Roberson
Walter Roberson on 7 Jun 2018
It was @table up to R2016a and became @tabular in R2016b
The easiest way to check for any system is to use which:
T = table(123);
which disp(T)

Sign in to comment.

More Answers (0)

Categories

Find more on Tables 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!