perform operations next to a string in matlab - string

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
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
Is there a simpler way of doing this

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

Related

Manipulate and convert item qty data in Excel

I need to calculate the quantity of a new item, given conversion factors of the original item data. the 'NewQty' column is what I'm trying to solve for. Is there a formula in Excel that would perform this entire calculation?
This might help- from another post, breaking out the UOM string columns has been solved, linked here.
The first row for example:
OrigItem has 25 CA's. You first want to convert it to its lowest Unit of Measure (EA). 'OrigUOMString' says that there are 30 EA's in a CA, so step 1:
25 (OrigQty) * 30 (OrigUOMString) = 750 EA
For the new item, there are 40 EA's in a CA, so step 2:
750 / 40 (NewUOMString) = 18.75
So the result says that qty of the new item would be 18.5 CA, which is the result that would go into 'NewQty'.
The second row for example:
13 BX's, so 12 (from OrigUOMString) * 13 (OrigQty) = 156 EA's
156 / 40 CA (NewUOMString) = 3.9 CA's (which goes into column 'NewQty')
OrigItem#
OrigUnitOfMeasure
OrigQty
OrigUOMString
NewItem#
NewUOMString
NewUOM
NewQty
111xy
CA
25
1EA/2PK/12BX/30CA
ABC123
1EA/4PK/20BX/40CA
CA
?
111xy
BX
13
1EA/2PK/12BX/30CA
ABC123
1EA/4PK/20BX/40CA
CA
?
In cell H2 you can try the following (I assume you don't have any excel version constraint per tags listed in the question)
=LET(rng, A2:G3, oUnit, INDEX(rng,,2), oQTY, INDEX(rng,,3),
oStr, INDEX(rng,,4), nStr, INDEX(rng,, 6), nUnit, INDEX(rng,,7),
GET_UNIT, LAMBDA(x,unit, 1*TEXTAFTER(TEXTBEFORE(x, unit), "/", -1)),
MAP(oUnit, oQTY, oStr, nStr, nUnit,LAMBDA(oUn,qty,oSt,nSt,nUn,
LET(m, GET_UNIT(oSt, oUn), d, GET_UNIT(nSt, nUn),
(qty * m)/d)))
)
and here is the output:
The main idea is to create a LAMBDA function (because we are going to use it more than once): GET_UNIT to extract the unit information. We extract the text before the unit via TEXTBEFORE, and then we get the sub-string after the last instance of / via TEXTAFTER. The rest is just to use MAP to iterate over all rows and then use the multiplier (m) and the divisor (d) to do the calculation.

Excel multi tiered lists in data validatation using alphanumeric fields

I am using multi tiered lists and data validation and have an issue when the fields are alpha numeric
eg
Col 1 Col 2
100 Dairy 101 Milk
102 Cheese
200 Bakery 201 Bread
202 Cake
If you choose 100 Dairy in the drop down list Col1 Col2 will not give the 2 fields to choose from however if you remove one of the sets of numbers then data validation will provide the 2 options in col 2
In Column 2 Data validation I am using =Indirect(A1)
How can I have both Number and letters in both of my drop down lists
Any assistance appreciated
A Name (named range) cannot start with a digit or contain spaces. So, in your validation formula, you have to change your entry in column A into the actual name that is being used for your dependent dropdown.
If you used the Excel Create Name wizard, then something like:
=INDIRECT(SUBSTITUTE(" " & $A$1," ","_"))
That will only work if ALL of your dependent lists start with a digit. If not, you will need a different algorithm.
eg:
=INDIRECT(SUBSTITUTE(IF(ISNUMBER(-LEFT($A$1))," ","")&$A$1," ","_"))
If you made up your own names, again, you will need a different algorithm.

Lookup and replace specific values with reference in pivot table

If I'm trying to find specific names in a list given from my pivot table such as -
Row Labels Revenues Order #
Panera 25 0
Pasta 15
Salad 10
Olive Garden 40 0
Sandwich 20
Pasta 20
Panda Express 30 0
Rice 15
Chicken 15
And I want to search through my document, find Olive Garden and Panda Express and I wanted to replace the 0 in the order # column with 10 for Olive Garden and 20 for Panda Express. Currently, someone here helped me out with
=IF(IFERROR(VLOOKUP(A9,worksheet!K:K,1,FALSE),"")="","",0)
which inserts 0's for the headers and blanks for the orders in the 'Order #' column, can I add a second formula that would find the names and replace the value in that column? Or adjust the current formula?
Quick note - order # column is not from the pivot table.
To make it more clear, - I am getting data from an external source (i.e. paper invoices), as opposed to making a manual entry to adjust the 0's in the order # column, I would like to tell VBA/Excel - "hey Olive Garden's order number is 10 and Panda Express's order number changed to 20, adjust".
this is my end goal -
Row Labels Revenues Order #
Panera 25 0
Pasta 15
Salad 10
Olive Garden 40 10
Sandwich 20
Pasta 20
Panda Express 30 20
Rice 15
Chicken 15
If you have a range with the restaurant names in one column and the order numbers in the next column (say columns X and Y of the sheet called "worksheet"), you could change your formula to be
=IF(IFERROR(MATCH(A9,worksheet!K:K,0),"")="","",IFERROR(VLOO‌​KUP(A9,worksheet!X:Y‌​,2,FALSE),0))
(P.S. Changed the original VLOOKUP to MATCH based on useful feedback from teylyn.)
FWIW, that formula would be better using MATCH, not VLookup, since it's returning the value from the first column.
But back on track: what are you trying to achieve? Change the values in a pivot table?
First, a formula cannot change values in another cell.
Second, a pivot table reports on existing data. You can't change the numbers that a pivot table reports.
You will need to re-think your approach. If you don't like the numbers the pivot table returns, you'll need to change the underlying source data.

Excel with rows of details related to other row

There is a way to have a group of rows related to other one, in the same sheet, like a more detailed information? Obviously must keep them always next to the main row if you filter or sort.
Desired example based on vehicles and travels:
A B C D
1 [ID] [VEHICLE TYPE] [BRAND] [COLOUR]
+ 2 A-171 PICKUP HONDA BLACK
- 3 [TRAVEL] [KM] [STATION]
- 4 12/08/2016 13.000 BARCELONA
- 5 13/08/2016 13.750 DONOSTI
+ 6 B-501 VAN RENAULT WHITE
- 7 [TRAVEL] [KM] [STATION]
- 8 12/08/2016 117.800 PARIS
- 9 13/08/2016 120.000 AMSTERDAM
- 10 14/08/2016 124.320 MUNICH
So when you sort the spreadsheet, should keep always the travel rows next to the vehicle row.
It is that possible? If not, what can I do to get this or similar? (I don't mind to use other sheet tab, but it wasn't the ideal)
You can use the Group function (Alt-A-G-G), and they won't be sorted as usual if you use sort on the whole column

How to sum the number of overlapping time frames in excel

Hoping someone can help,
I work for a fire department and i am trying to determine the number of times all our rigs on the road at emergencies at the same time. I have all the data from date, times, etc... So what i am looking for is an excel summation total that would display the sum of overlapping times that were greater than 3. So kinda as follows:
Rig Date Start Time End Time
1 1/1/2015 0703 0759
2 1/1/2015 0705 0823
3 1/1/2015 0706 0815
4 1/1/2015 0723 0759
1 1/1/2015 0802 0845
With more than three rigs on the road after 0723 it would grant me a total of one but then at 0802 rig 1 goes back out again meaning my total would increase by 2 and so on and so on. I dont have the slightest clue as to how to program this. I have three years of data i need to crunch through and something like this would help me greatly. Any help whatsoever is appreciated. Thanks in advance and lets see what you all come up with!
First, you might want to convert the times to Excel date-times (assuming you already applied Format as Table to your data, so all new formulas get populated for all rows automatically):
if the values are already Excel times, use:
=$B2 + C2
if you have integers like "703" (formatted as "0703"), use:
=$B2 + INT(C2/100)/24 + MOD(C2, 100)/24/60
or if you have values-as-text:
=$B2 + LEFT(C2, 2)/24 + RIGHT(C2, 2)/24/60
Don't forget to format the new columns using Custom Number format d.m. hh:ss or m/d hh:ss.
The next step is to count all ongoing deployments that end only after the current deployment started, i.e. use following formula as illustrated on screenshot:
=COUNTIFS(F$2:F2, ">" & E2)
Please make sure that the formula e.g. in row 13 looks like =COUNTIFS(F$2:F13, ">" & E13) to check that you are on the right track. Also none of the values can be 5 or more if you only have 4 rigs, otherwise you have an error in your data.
And to count number of times when all 4 rigs were in use, the formula looks like this:
=COUNTIFS($G$2:$G$13, 4)
In case you also want to sum the time while none rigs were available, add 2 more columns in your table. Column H in my illustration needs following array formula (entered by Ctrl+Shift+Enter):
=IF(G2=4, MIN(IF(F$2:F2 > E2, F$2:F2)), "")
And a normal formula in column I:
=IF(G2=4, H2-E2, "")
Don't forget to format numbers accordingly.
And do a simple sum: =SUM($I$2:$I$13).

Resources