1

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. 

end

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?

2
  • So, what is the problem? Getting rid of eval? Commented Dec 2, 2013 at 8:17
  • Yes, I'm trying to get rid of eval within the parfor loop Commented Dec 3, 2013 at 3:08

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, I tried implementing this approach, but I keep getting this error: "Attempt to reference field of non-structure array"
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
I solved it using your approach, Many thanks. Ben

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.