how to store an output in a function (or bring out the stored output into the workspace) for n-number of model run

조회 수: 3 (최근 30일)
I have a hydrological model which runs N number of times to calibrate the parameters.
For each model run, I have a following function where it grabs the modeled simulated output, as well as, NSE output for that run.
function [SimRR, NSE]=HYPE(x)
SimRR = dlmread('output.txt','\t','I2..I100');
NSE_num = importdata('subbasin2.txt','\t');
NSE=NSE_num.data([2]); %NSE value is located in the second spot
end
Everything runs fine. Here, I'd like to store each NSE value for N number of times. (eg. if I'm running the model 3000 times, then I would be having 3000 NSE values stored in an array).
I tried using 'assignin' to bring the value to the base and it only stores NSE value of the last run. If there is a way to store an output generated from a function, please let me know.
If you have any further questions, feel free to comment and I'll provide you with more details.

답변 (1개)

Ken Atwell
Ken Atwell 2019년 10월 29일
Assuming NSE is a scalar double (that is, a single number), When you call HYPE, you can append the NSE result to an (intially empty) vector:
NSE=[];
for i=1:3000
% ...
[SimRR, NSE(end+1)]=HYPE(x);
end
% NSE should be length 3000
  댓글 수: 2
Jung
Jung 2019년 10월 29일
Is there a way to store the values within a function (maybe a table ) and call it out later?
The structure of this hydro model script that I'm using is a little different that what I'm used to so I try not to touch the initial interface as much as possible.
Ken Atwell
Ken Atwell 2019년 10월 29일
You can using a persistent variable who's value will "survive" across calls to the function. That said, preserving state inside a function is considered bad form -- very nearly a global variable -- that is best avoided if you can.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Beamforming and Direction of Arrival Estimation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by