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

조회 수: 2 (최근 30일)
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
Bruce Elliott
Bruce Elliott 2018년 6월 7일
편집: Bruce Elliott 2018년 6월 7일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by