简体   繁体   中英

matlab, how to use the output that is printed by a function?

I would like to store some values that are printed during the iterative procedure of a function, but I have no idea how. here is the code I am using:

a = 0
b = 2
tol = 1.e-6
trace = 1
F = @(x)1./(x.^3-2*x-5);
Q = quad(F,a,b,tol,trace);

the quad funciton gives the integral of F from a to b using the adaptive quadrature method. trace = 1 prints the values to the console [fcnt a ba Q] during the recursion, but does not store them to the variable.

I would like to store the values a and ba that are printed during the procedure, for later use. for instance, this code gives

>> quad(F,0,2,1.e-6,1);
   9     0.0000000000     5.43160000e-01    -0.0989460227
  11     0.5431600000     9.13680000e-01    -0.1584111746
  13     0.5431600000     4.56840000e-01    -0.0755952309
  15     1.0000000000     4.56840000e-01    -0.0828028464
  17     1.0000000000     2.28420000e-01    -0.0391911692
  19     1.2284200000     2.28420000e-01    -0.0436112507
  21     1.4568400000     5.43160000e-01    -0.2054245169
  23     1.4568400000     2.71580000e-01    -0.0667670196
  25     1.4568400000     1.35790000e-01    -0.0302481864
  27     1.5926300000     1.35790000e-01    -0.0365183194
  29     1.7284200000     2.71580000e-01    -0.1366285551
  31     1.7284200000     1.35790000e-01    -0.0492888403
  33     1.8642100000     1.35790000e-01    -0.0871164919
  35     1.8642100000     6.78950000e-02    -0.0350033472
  37     1.9321050000     6.78950000e-02    -0.0520998049
  39     1.9321050000     3.39475000e-02    -0.0228214919
  41     1.9660525000     3.39475000e-02    -0.0292778809

I need the values printed in the second and third columns above.

Thank you.

I'm not sure I've understood your question; if you want to store the trace values

9 0.0000000000 5.43160000e-01 -0.0989460227 11
0.5431600000 9.13680000e-01 -0.1584111746

etc ...

into an array, consider that the trace values are printed by the quad funcition by using the fprintf .

You can edit the quand function - edit quad - and see:

if trace
    fprintf('%8.0f %16.10f %18.8e %16.10f\n',fcnt,a,h,Q);
end

I see at least two possibilities:

1) Use the diary function

You can modify your code by calling diary right before calling quad ; this function create a log of the ouput displayed in the CommandWindow into a text file.

Then, you can load the content of that file to impport its content (the trace data) in the Workspace.

Do not forget to add ";" at the end of the call to quad otherwise also the output of the function will be stored into the diary file and this will prevent the possibility of loading it

a = 0
b = 2
tol = 1.e-6
trace = 1
F = @(x)1./(x.^3-2*x-5);
% Define the name of the diary file
diary_filename='trace_data.txt';
% Enable saving the data into the "trace_data.txt" output file
diary(diary_filename)
Q1 = my_quad(F,a,b,tol,trace);
% Turn off the diary log
diary
% Load the trace data into the "trace_data" array in the workspace
trace_data=load(diary_filename)

You might have a more "general" approach and dynamically generate the trace data output file, by using tempname .

( tempname generate the filename in the temporary folder, so,if you want to store it into you current directory you have to split it, extract the actual filename by using fileparts )

a = 0
b = 2
tol = 1.e-6
trace = 1
F = @(x)1./(x.^3-2*x-5);
% Dynamically generation of the output file name
tmpName = tempname
% Extract the actual filename
[pathstr,name,ext]=fileparts(tmpName)
% Build the filename and add the extension
diary_file=[name '.txt']
% Enable saving the data into the "trace_data.txt" output file
diary(diary_file)
Q1 = my_quad(F,a,b,tol,trace);
% Turn off the diary log
diary
%  Load the trace data into the "trace_data" array in the workspace
trace_data=load(diary_file)

2) Modify the quad function

Since the source code of the quad function is available, you can either directly modify the function (not recommended) or copy it in a folder on your path and modify it.

There are many way to modify the function.

One of them could be to:

  • add an input parameter in which you can specify the name of the output file
  • add in the function the code to open the file ( fopen )
  • add the file handle in the fprintf call
  • close the output file at the end ( fclose )

another possibility could be to add an output parameter in the definitin of the function in which to store the trace data; in thsi case you also have to add the code to store the trace data into an array at each iteration of the function.

Hope this helps.

Qapla'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM