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

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

我想存储一些在函数迭代过程中打印的值,但我不知道如何存储。 这是我正在使用的代码:

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);

quad 函数使用自适应求积法给出 Fab 的积分。 trace = 1 在递归期间将值打印到控制台 [fcnt a b-a Q],但不将它们存储到变量中。

我想存储在过程中打印的值 a 和 b-a 以备后用。 例如,此代码给出

>> 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

我需要上面第二列和第三列中打印的值。

谢谢。

我不确定我是否理解你的问题;如果你想存储 trace

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

etc ...

到数组中,考虑 trace 值是由 quad 函数使用 fprintf.

打印的

您可以编辑 quand 函数 - edit quad - 并查看:

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

我至少看到两种可能性:

1) 使用diary函数

您可以在调用 quad 之前调用 diary 来修改您的代码;此函数将 CommandWindow 中显示的输出日志创建到文本文件中。

然后,您可以加载该文件的内容以将其内容(跟踪数据)导入工作区。

别忘了加上“;”在对 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)

您可能有更“通用”的方法,并通过使用 tempname.

动态生成跟踪数据输出文件

(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)修改quad函数

由于quad函数有源码,你可以直接修改函数(不推荐)或者复制到你路径下的文件夹中并修改它。

修改函数的方式有很多种。

其中之一可能是:

  • 添加一个输入参数,您可以在其中指定输出文件的名称
  • 在函数中添加打开文件的代码(fopen)
  • fprintf 调用中添加文件句柄
  • 最后关闭输出文件(fclose)

另一种可能性是在存储跟踪数据的函数的定义中添加一个输出参数;在这种情况下,您还必须添加代码以在函数的每次迭代中将跟踪数据存储到数组中。

希望这对您有所帮助。

Qapla'