簡體   English   中英

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函數使用自適應正交方法給出abF積分。 trace = 1在遞歸過程中將值打印到控制台 [fcnt a ba Q], 但不將它們存儲到變量中。

我想存儲在過程中打印的a和ba值,以備后用。 例如,這段代碼給出了

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

等...

放入數組中,請考慮使用fprintfquad函數打印trace值。

您可以編輯quand功能- edit quad -看看:

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

我至少看到兩種可能性:

1)使用diary功能

您可以在調用quad之前立即通過調用日記來修改代碼; 此函數將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

另一種可能是在存儲跟蹤數據的函數的definitin中添加一個輸出參數; 在這種情況下,您還必須在函數的每次迭代中添加代碼以將跟蹤數據存儲到數組中。

希望這可以幫助。

卡普拉

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM