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

2 visualizzazioni (ultimi 30 giorni)
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 Commento
Bruce Elliott
Bruce Elliott il 7 Giu 2018
Modificato: Bruce Elliott il 7 Giu 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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 6 Giu 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 Commenti
Bruce Elliott
Bruce Elliott il 7 Giu 2018
Yes, except that on Windows it's "@table\'. I assume that the '@tabular/' is a Mac thing (?).
Walter Roberson
Walter Roberson il 7 Giu 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)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by