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

3 visualizaciones (últimos 30 días)
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 comentario
Bruce Elliott
Bruce Elliott el 7 de Jun. de 2018
Editada: Bruce Elliott el 7 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by