perform operations next to a string in matlab

43 views Asked by At

I have my data coming back as a cell which looks like this (each trade is underneath each other in the cell ie its a 5 by 4 cell):

sell      50    FTSE                   6500 
buy       100   Eurostoxx              3300  
buy       25    SP                     1980 
buy       30    FTSE                   6490
sell      25    Eurostoxx              3315  

Firstly because I have mixed data its coming back from my database query as a cell so not sure if thats a problem

What I'd like to do is mark these to market, so I have closing market price of each of these contracts as a variable lets call them FTSE_CLOSE, EUROSTOXX_CLOSE and SP_CLOSE.

I'd like to be able to do something that goes as follows:

where coloumn 3 = "FTSE" and column 1 = "sell" then column 5 = column2 * (column4 - FTSE_CLOSE)
where column 3 = "FTSE" and column 1 = "buy" then column 5 = column2 * (FTSE_CLOSE - column4)  

and so on for the other contracts.

Basically I am struggling with the mixed use of strings and numbers

  1. Do I need to cell2mat everything so I have several vectors and then somehow look into the string vectors and perform the calucaltion on the other relevant vectors i.e. quantity * price - close_price
  2. Is there a simpler way of doing this
1

There are 1 answers

0
moink On

I think you are simply looking for the command str2num

Here is one idea:

input_data={"sell","50","FTSE","6500";
            "buy","100","Eurostoxx","3300";
            "buy","25","SP","1980",
            "buy","30","FTSE","6490",
            "sell","25","Eurostoxx","3315"};

closing_price.FTSE=6000;
closing_price.Eurostoxx=3000;
closing_price.SP=2000;

for i=1:size(input_data,1)
    if strcmp(input_data{i,1},'sell')
       diff_from_market(i)=str2num(input_data{i,2}) ...
           *(str2num(input_data{i,4})-closing_price.(input_data{i,3}));
    elseif strcmp(input_data{i,1},'buy')
       diff_from_market(i)=str2num(input_data{i,2}) ...
           *(closing_price.(input_data{i,3})-str2num(input_data{i,4}));
    end  
end
diff_from_market