添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to execute a parfor loop within a parent script for Matlab.

I want to calculate the implied volatility of an option price, and then create a new column within a preexisting dataset with the results.

   load('/home/arreat/Casino/names.mat') 

name = char(names(i))

%Loop over n rows to populate columns in dataset named using variable 'name(i)'
rows = eval(['length(',name,')'])
    parfor n=[1:rows]
%Calculate implied volatility using blsimpv(Price, Strike, Rate, Time, Value, Limit,Yield, Tolerance, Class)
BidIV = blsimpv(eval([name,'.UnderlyingPrice(n)']),...
eval([name,'.Strike(n)']),...
RiskFree/100,...
eval([name,'.Lifespan(n)'])/252,...
eval([name,'.Bid(n)'])+.01,...
10,...
0,...
1e-15,...
eval([name,'.Type(n)'])...
eval([name,'.BidIV(n,1) = double(BidIV);']);
%Loop and add implied volatility (BidIV) to a column with n number of
%rows. 

The problem arises with the 'eval()' calculation in the parfor loop. Mathworks suggested that I should turn the whole script into a function, and then call the function within the parfor loop.

While I work on this, any ideas?

Instead of calling eval all the time, you can call it once outside the loop, e.g. data = eval(name), and then use data.Strike etc inside the parfor loop.

To avoid calling eval at all, do the following:

 %# load mat-file contents into structure allData, where 
 %# each variable becomes a field
 allData = load('/home/arreat/Casino/names.mat');
 data = allData.(name);
                Okay, I tried implementing this approach, but I keep getting this error:   "Attempt to reference field of non-structure array"
– BenTam64
                Dec 3, 2013 at 3:02
                load('/home/arreat/Casino/names.mat');        %names.mat is a (2853 X 1 cell);  i = 1;  name = char(names(i));  rows = eval(['length(',name,')']);  named = eval('name');       parfor n=[1:rows];    BidIV(n,1) = blsimpv(named.UnderlyingPrice(n),...  named.Strike(n),...  RiskFree/100,...  named.Lifespan(n)/252,...  named.Bid(n)+.01,...  10,...  0,...  1e-15,...  named.Type(n)...   );      end;     This is the code
– BenTam64
                Dec 3, 2013 at 3:03
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.