How can I capture the lines written to the CLI by the disp() function?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
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:
- Call disp(myTable) to display the table in the command line interface.
- Select the displayed lines and copy them to the system clipboard.
- 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.
채택된 답변
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
It worked in my test, on a Mac.
Did you examine the file in NotePad or in WordPad or other editor? NotePad would need \r\n -- but if you opened with 'wt' then the \n characters should have been translated.
I did open the file with 'wt' and then viewed it in Notepad++, Notepad, Wordpad, and the Matlab editor, all with the same results (no explicit \n, but markup tags <strong> and </strong>). I even tried opening it in a web browser (Chrome) just to see if it would recognize the tags, but it didn't.
I'm guessing that the markup is something specific to the Matlab CLI, where the table variable names and underscores are displayed in boldface. That's probably reason enough that there is no quick and easy way to produce similar output to a text file.
When you say "no explicit \n", do you mean that you are expecting to see the literal character "\" followed by "n" in the output file? Because the \n I referred to is shorthand for referring to newline characters, char(10), which are in the file.
Opening the file in a browser would not recognize the tags because the file does not have an '<HTML>' header. MATLAB's command window has an implicit HTML in effect.
A couple of months ago I chased down how to turn off the HTML for one particular variety of tables; see https://www.mathworks.com/matlabcentral/answers/399283-how-can-i-get-plain-text-diary-files#answer_318884
In any case, to turn off the strong tags when displaying a table, use
result = evalc('disp(NameOfTableGoesHere, false)');
Bruce Elliott
2018년 6월 7일
편집: Bruce Elliott
2018년 6월 7일
- Yes, I had thought that you'd meant that the string literal '' would appear in my text file, so that's what I was looking for. I get it now.
- Yes, I knew that the lack of an '<HTML>' tag would pretty well guarantee that the other tags would not be interpreted by a browser, but I wanted to check in case there was some hidden magic that I wasn't aware of.
- Thanks for the link to the earlier question. That's very similar to my question, in that he wanted to mimic the results of copying and pasting the table output as displayed on the CLI. (See my comment to my original question above, if you haven't seen it yet.)
- !!! What is that second argument to the disp() function?? I can't find it in any documentation, but it appears to be exactly what I was looking for. Using that single line of code plus an fwrite or fprintf, I should get exactly what I wanted (!). EDIT: I've found the code for the overridden disp method of the table class, and now I see how the optional arguments are used.
Thank you!
Yes, as you discovered this is controlled by toolbox/matlab/datatypes/@tabular/disp.m
Yes, except that on Windows it's "@table\'. I assume that the '@tabular/' is a Mac thing (?).
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개)
카테고리
도움말 센터 및 File Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
