Wednesday, June 22, 2011

Barker's Code

Following Suit with Sisi, I thought it would be best to provide an example that works with text rather than numbers.

As we have said a number of times now, strings are often more difficult to work with, and yet they are often more intuitive than if you were to use a series of numbers to describe your data. For instance, I could describe two groups of rats as "0" and "1" or "low-dose" and "high-dose". To an individual than knows the code, both are essentially the same. However, to the naive onlooker, the amount of information provided by the second example is far greater.

Learning to work with strings is a powerful tool for data analysis. Moreover, as we continue to work with Matlab and Excel, you will likely realize that Matlab is not as "string-friendly" as excel, and that tasks involving strings are often better accomplished in excel. In fact, we will often use excel to convert strings to numbers before importing the data into Matlab.

The nested code I have written for you is as follows:

'=IF(AND(ISTEXT(A5),ISTEXT(B5)),CONCATENATE(A5," ",B5),"ERROR!")'

Lets break this down into a few pieces.


1) The deepest layer of this function is the "ISTEXT" command. Like many logical commands in excel this is designed to return either TRUE or FALSE, depending on whether or not a given criterion has been met.

In this case, ISTEXT is simply asking "Is the content of the referenced cell text?" and returning TRUE if the answer is YES, and FALSE if the answer is NO.

2) The two instances of ISTEXT in the above formula are embedded within the "AND" function. This is another logical function that returns TRUE if both values inside of the function are TRUE, and FALSE if either one, or both of the functions are FALSE.

To give you a more colloquial example, the function (in my formula) is asking "Was the answer to BOTH the first AND second intext command TRUE?".

3) While the "IF" function is the outermost layer of my nested function, it needs to be explained before the last function in order to make the whole thing easier to comprehend.

The IF function has 3 inputs (i.e."=IF(INPUT1, INPUT2, INPUT3)").

The first input is your logical criterion. In my example, the logical criteria is the the AND function described above. Thus, if both cells A5 and B5 contain text, the value would be "TRUE" and the criterion has been met.

The second input is the value you want the function to return when the function is TRUE. In this case, I have designated that the concatenate function be called when the function is true. The result of this function is described below.

The last output is the value to return when the criterion has NOT been met (i.e. is FALSE).

For example, if I created the function "=IF(5=4,"TRUE","FALSE")", the formula would return the string FALSE, because the numbers 5 and 4 are not equal. However if I modified the formula to "=IF(5=5,"TRUE","FALSE")", the string TRUE would be returned.

Returning to the main example, the value I have designated if cells A5 and B5 are NOT both strings is "ERROR!".

4)Finally, the fourth function in my nested function is the "CONCATENATE" function. This function is used to combine strings together. Therefore, when the criterion for the IF function is TRUE, the CONCATENATE function combines the strings in A5 and B5. To make things prettier I have even inserted a space by concatenating A5," ", and B5. As you can probably see, the middle string is nothing more than a space.

No comments:

Post a Comment

Thoughts? Leave a comment!