简体   繁体   English

matlab,如何使用函数输出的输出?

[英]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. quad函数使用自适应正交方法给出abF积分。 trace = 1 prints the values to the console [fcnt a ba Q] during the recursion, but does not store them to the variable. trace = 1在递归过程中将值打印到控制台 [fcnt a ba Q], 但不将它们存储到变量中。

I would like to store the values a and ba that are printed during the procedure, for later use. 我想存储在过程中打印的a和ba值,以备后用。 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 如果要存储trace

9 0.0000000000 5.43160000e-01 -0.0989460227 11 9 0.0000000000 5.43160000e-01 -0.0989460227 11
0.5431600000 9.13680000e-01 -0.1584111746 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 . 放入数组中,请考虑使用fprintfquad函数打印trace值。

You can edit the quand function - edit quad - and see: 您可以编辑quand功能- edit quad -看看:

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 1)使用diary功能

You can modify your code by calling diary right before calling quad ; 您可以在调用quad之前立即通过调用日记来修改代码; this function create a log of the ouput displayed in the CommandWindow into a text file. 此函数将CommandWindow中显示的输出的日志创建到文本文件中。

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 在对quad的调用结束时,否则该函数的输出也将存储到日记文件中,这将防止加载它的可能性

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动态生成跟踪数据输出文件。

( 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 ) tempname在临时文件夹中生成文件名,因此,如果要将其存储到当前目录中,则必须对其进行拆分,请使用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 2)修改quad功能

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. 由于可以使用quad函数的源代码,因此您可以直接修改该函数(不建议使用) ,也可以将其复制到路径上的文件夹中并进行修改。

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 ) 在函数中添加代码以打开文件( fopen
  • add the file handle in the fprintf call fprintf调用中添加文件句柄
  • close the output file at the end ( fclose ) 最后关闭输出文件( fclose

another possibility could be to add an output parameter in the definitin of the function in which to store the trace data; 另一种可能是在存储跟踪数据的函数的definitin中添加一个输出参数; 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' 卡普拉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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