Saturday, November 18, 2017

How to Use Excel Pivot Tables to Summarize Continuous Data

Suppose you have a set of data points that represent some quantity in a volume.  It could be temperature in a solid, for example.  How can you use a Pivot Table find the average temperature in a region on the solid’s surface?

Well, if you’re lucky, the region is rectangular, bounded by some Xmin and Xmax and Ymin and Ymax.  Then you can use the built-in Label Filters.

I created a set of data for this article with -20<=X<=20, -20<=Y<=20, -20<=Z<=20.  The temperature is a function of X,Y,Z using the (arbitrary) formula =2.5*X+0.25*Y^2+2.5*SIN(Z).

First, set up the Pivot Table and chose the option to Allow multiple filters per field.


In the screenshots below, we define a Region with Label Filters in the PivotTable Rows within X=[-2,8] and Y=[5,7].  The function is not dependent on the Z coordinate – we’ll use a Page Filter to constrain Z to 0.  As can be seen in the status line of the fourth screenshot below, the average temperature in that region is 16.67 degrees C.






But suppose the region is more complicated than a rectangle, or you want to summarize multiple regions?

You can define a function in VBA to “discretize” the data, as shown in the code sample, below.
Option Explicit

Function Region(rngX As Range, rngY As Range, rngZ As Range) As String
Const regA_Xmin = -2#: Const regA_Xmax = 8#
Const regA_Ymin = 5#: Const regA_Ymax = 7#
Const regB_Xmin = -6#: Const regB_Xmax = -3#
Const regB_Ymin = 5#: Const regB_Ymax = 7#
Const regC_Xmin = -2#: Const regC_Xmax = 8#
Const regC_Ymin = -5#: Const regC_Ymax = -3#
Const regD_Xmin = 9#: Const regD_Xmax = 12#
Const regD_Ymin = 5#: Const regD_Ymax = 7#
Const regE_Xmin = 16#: Const regE_Xmax = 18#
Const regE_Ymin = -15#: Const regE_Ymax = -7#
Dim x As Double, y As Double, z As Double

x = rngX.Value: y = rngY.Value: z = rngZ.Value

If x >= regA_Xmin And x <= regA_Xmax And _
   y >= regA_Ymin And y <= regA_Ymax Then
        Region = "Region A"
ElseIf x >= regB_Xmin And x <= regB_Xmax And _
       y >= regB_Ymin And y <= regB_Ymax Then
        Region = "Region B"
ElseIf x >= regC_Xmin And x <= regC_Xmax And _
       y >= regC_Ymin And y <= regC_Ymax Then
        Region = "Region C"
ElseIf x >= regD_Xmin And x <= regD_Xmax And _
       y >= regD_Ymin And y <= regD_Ymax Then
        Region = "Region D"
Else
    Region = "NA"
End If

End Function

The function accepts three Range type parameters, each of which are intended to be the address of a single cell.  It returns one of five strings: Region A, Region B, Region C, Region D, NA.

The first four constants that are defined in the first two lines are minimum and maximum X and Y coordinates for the region that were defined in the previous example with Label Filters.  So the string “Region A” is returned if X and Y are within those X-Y intervals.

When we type in cell E2 =Region(A2, B2, C2) and then fill down, Excel calls the function for each of the cells that the formula occupies and then puts the result in that cell.  It can take several seconds for all the cells to be populated, even with an i5 processor.  And the calculation can increase the drain on the battery if you’re doing this unplugged / on the road.

But when it’s complete, you can set up your Pivot Table with the regions in the row.  The average of the temperature values for Region A is shown in cell B5 below as 16.67.



Wednesday, November 01, 2017

Introduction to CLCL Tools

CLCL is the clipboard manager that I prefer.  But it’s lacking in extensive documentation, at least for those who don’t know Japanese.  I figured out how to configure the Tools that it comes with, so I thought I’d share that with you.

This procedure assumes that you already know how to download, install and run the main program.

Download the Tools from http://www.nakka.com/soft/clcl/index_eng.html .  There are two ZIP files (tltxt002_eng.zip and tlutl002_eng.zip, as of this writing) that provide DLLs with the functions that serve as tools.  Copy the DLLs from those ZIP files into the directory in which the CLCL executable resides.  While you’re at the Nakka website, you might as well download the other ZIP files that provide ability to manage as wider range of file formats.  Copy their DLLs, too.  As of this writing, there are two: fmtrtf002_eng.zip; fmtmeta002_eng.zip .

Invoke Tools from the Viewer menu, then choose Tool Setup…


Click the Add… button.

In the DLL textbox, enter the path and filename of one of the tool DLLs, for example, C:\Program Files (x86)\CLCL\tool_text.dll .  If you click the Browse button, you can use a File Dialog box to navigate to and select the DLL.

Now choose one of the Tools to add.  I find &Edit to be useful, so choose that.  OK your way back out.

Now when you press your hot key to call up the clipboard list, you can right-click on a text entry, then click on Edit to open a simple editor to modify the text.  When you’re done, you can click OK to put the edited content onto the clipboard, replacing the original entry.





Use the same procedure to try out the other tools.

Tuesday, October 31, 2017

Where Should You Stick Your Function?

Let's say you need to create a VBA function to use in a spreadsheet. Where do you put it? Quick answer, put it in a module.

In Excel, press Alt-F11, Ctrl-R (to set focus to the Project Explorer). Right click on VBAProject (xls_name), where xls_name in the file name of your workbook. Select Insert Module.

A blank code window should appear and the cursor should be positioned in it.  This is "Module1" where you can enter your code, such as...

Option Explicit
Function FortyTwo(r As Range)
If r.Value = 42 Then
    FortyTwo = "Forty-Two"
Else
    FortyTwo = r.Value
End If
End Function


If you enter 1, apple, and =6*7 in cells A1, A2 A3, here's what you get as output if you enter =FortyTwo(A1) in cell B1 and drag down to B3:







Of course, if A3 contained =6*9 you should also get Forty-Two, but that's a programming challenge for another day.

Sunday, July 02, 2017

The Dreaded Non-Disclosure / Non-Compete Agreement

If you work in the USA tech industry, you've probably been asked to sign a non-disclosure / non-compete agreement with a new employer.  It may be that even your current employer asked you to sign a new one.

If you're not familiar with it, let me describe it for you in a nutshell: Anything you witness or produce on company time becomes the property of your employer.  Even if you think of something, if you thought of it on company time, that idea is your employer's property.  This explains a lot of what I've observed during my career -- many coworkers are so poignantly aware of this that the second they clock in, they empty their minds completely of thoughts and go about their duties thoughtlessly.

If asked to sign a non-disclosure / non-compete agreement, what should you do?  This ebook from IEEE should help:
https://www.ieeeusa.org/members/IPandtheengineer.pdf

Good luck!

Sunday, June 04, 2017

Hard Drive Thrashing? Maybe It's Windows Update

In a previous post, I wondered if my computer problems were due to AntiVirus interfering with Windows Update.

Perhaps.  But there's definitely a problem with Windows Update. The following seemed to get rid of the excessive and endless thrashing of the hard drive:

http://www.cagedrat.com/microsoft-windows/microsoft-windows-updates-cab-files-filling-up-hard-drive/

And yes, I plug in my computer and set it to Stay Awake before downloading and installing updates.

Wednesday, May 17, 2017

Some Things Never Change

For over two thousand years, engineers had to contend with manufacturing issues.  We're still doing that today...

"... its design conception exceeded the engineering precision of its manufacture by a wide margin—with considerable cumulative inaccuracies in the gear trains, which would have cancelled out many of the subtle anomalies built into its design."

From the Wikipedia page "Antikythera mechanism," Google's feature for today, 2017-05-17.
https://en.wikipedia.org/wiki/Antikythera_mechanism

Sunday, April 23, 2017

Dummies Data Visualization Links

The following links are listed in Data Visualization for Dummies:

https://www.edwardtufte.com/tufte/
http://visual.ly/
http://chartporn.org/
https://excelcharts.com/posts/
http://flowingdata.com/

Seems to be more for artists than for engineers, though.