Can I write the output of a 'summary' command of a 'table'-variable to a variable ?
조회 수: 2 (최근 30일)
이전 댓글 표시
>> table(A,B,C)
ans =
A B C
_________________________ _________________________ _________________________
1 2 3 4 5 6 7 8 9
>> summary(ans)
Variables:
A: 1x3 double
Values:
A_1 A_2 A_3
___ ___ ___
min 1 2 3
median 1 2 3
max 1 2 3
B: 1x3 double
Values:
B_1 B_2 B_3
___ ___ ___
min 4 5 6
median 4 5 6
max 4 5 6
C: 1x3 double
Values:
C_1 C_2 C_3
___ ___ ___
min 7 8 9
median 7 8 9
max 7 8 9
댓글 수: 6
Steven Lord
2018년 12월 12일
Can you tell us a little more about how you would use that variable containing the text displayed by the summary method? Knowing that may allow us to find a way to achieve your goal without making a copy of the summary method that you would need to maintain and update with each new release as we update the summary method.
Luisa Liboni
2018년 12월 12일
I need to send information to people who do not code in Matlab so that they could see the missing information (NaT, NaN, undefined categories) and the counts of all categories, as well as the max, min and mean of numeric values. It should work as a detailed report about the table's columns. I know it would result in a long text file or csv file, however, they could process this in a word processing software/ csv processing software.
I think I manage to work this around by using the following code before calling the function summary:
diary summaryreport.txt;
summary(tab);
This prints everything that is outputed in the command window into a text file.
Anyway, It would indeed be very good if there was a way of doing this automatically with the summary method.
답변 (3개)
Stephen23
2015년 3월 12일
편집: Stephen23
2015년 3월 12일
As Guillaume states, the function summary uses fprintf everywhere, so it is not designed to output a variable. But we can hack the code a little...
The summary code can be re-written to do what you want. open the function and save a local copy. Do NOT edit the MATLAB file itself, and name the new file (and function) something different from summary. Then change the local copy by adding an fopen command at the start of the main function:
fid = fopen('temp_summary.txt','wt');
then replace every instance of fprintf(...) with fprintf(fid,...). You might need to pass fid to the local functions too: just add it to the input arguments, and pay attention to the editor warnings (which will tell you where this values is required). Finally at the end of the main function add fclose(fid);.
Running this function creates an Mfile which includes the complete summary text.
str = fileread('temp_summary.txt');
Bingo: str is the summary text.
댓글 수: 6
Stephen23
2018년 12월 12일
편집: Stephen23
2018년 12월 12일
@Luisa Liboni: you will have to do some digging around using the debugging tools. First check if that call works with the original summary file. Then set a breakpoint in the original summary file on that line. Run it. Check where that data is defined (use the editor). You might need to run the function several times and use the debugger to step through the functions... You will have to figure out what the difference is between the original function and your new copy, e.g. it might depend on some private functions. Dig around. Do not stop just because you get an error!
Walter Roberson
2018년 12월 12일
편집: Walter Roberson
2018년 12월 12일
You could get that kind of problem when copying Mathworks code, if the function relies upon private functions instead of on functions that are on the regular path, or if the code relies upon a private property of a class (since you would not be executing in the context of the class.)
Steven Lord
2018년 12월 12일
Okay, I understand your use case. How do you want to send this information to your people who are not MATLAB users? Copy and paste into an email, email it to them directly using the sendmail function, write it to a text file, publish it to a PDF / Word / HTML / LaTeX / etc. document, or some other approach?
Putting your call to summary inside a call to evalc would allow you to capture that text, though it does include the <strong> tag used to highlight the variable names.Here is some code that creates a table, summarizes it and captures the summary in a variable T, removes the tags used to highlight the variable names when the summary is shown in the Command Window, and copies that variable to the clipboard. Paste the contents of the clipboard into a document in the MATLAB Editor or your favorite text editor to see how that looks. You could write this to a text file using the normal file I/O functions in MATLAB, send it using sendmail, or do anything to it that you can do to a normal variable containing text.
% Make a sample table
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
% Capture the summary
T = evalc('summary(patients)');
% Remove the strong tags
T = erase(T, ["<strong>", "</strong>"]);
% Copy to the clipboard
clipboard('copy', T);
If you put the following three lines of code into a MATLAB script file in the MATLAB Editor, click the Publish tab of the Toolstrip, and press the Publish button you'll receive an HTML report. [Clicking the small triangle under the Publish button will let you publish to different formats, see this documentation page for more information.] For programmatic publishing, see the documentation for the publish function. Does the resulting HTML report look like something that your users could consume?
load patients
patients = table(LastName,Gender,Age,Height,Weight,Smoker,Systolic,Diastolic);
summary(patients)
댓글 수: 1
Walter Roberson
2018년 12월 12일
matlab.internal.display.isHot would, I think, be false in -nodesktop mode, so if you were running without desktop, evalc() would not need to remove the <strong> tags.
... probably easier to just use the code to strip out the tags.
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
