Transcript for:
Module 6 Video - writecell: Writing a Cell Array to a file: Part 2

Here we will continue our introduction to WriteCell that we began in the last video. In the previous video we saw how to use WriteCell to create a new workbook and write some information from cell arrays into that workbook. Here we added information about resistors in a part of the workbook. parts list to our Excel workbook. Now we need to add information about the capacitors in the parts list on a separate sheet. We already have all of that information stored in variables from from earlier and note here that the file component list.xlss already exists in our current folder. So now we simply need to create a new sheet labeled capacitors and write this information to that sheet. Here I will use a slightly different method to write that information just to illustrate that there are usually multiple ways of doing the same sort of thing. in MATLAB. First, let's combine those three cell arrays, C-value, C-volt, and C-quant, into a single three-row cell array. Normally, we would use the concatenation operator, square brackets, as shown here, to accomplish this task, and we will discuss the concatenation operator in the next video. Here, however, I'm going to use a left-hand less common approach using curly braces to solve this problem, primarily to review curly braces and to point out a couple of pitfalls. There's our command. There is our variable in our workspace. Now let's take a look at what's in there. There we have it. The first row contains the values and farads, the second the voltage ratings, and the third the number of each that we need. Now let's go back to our command window. Some of you may be looking at that and say, what the heck are all of those curly braces for? Well, we're going to do a little bit of review of the syntax of using cell arrays because this is one of those topics that a lot of beginning students seem to struggle with. So we're going to go over it just a little bit here. First of all, remember Remember the mnemonic curly for contents. When you use curly braces, it pulls the contents out of the cell array. When you use parentheses, it gives you the same type of thing as what appears within that variable. So let's see what would happen if we leave some of these curly braces off and talk about why it does what it does. remove the outer curly braces. There we have it. So now we've just got our three cell arrays without enclosing all three in curly braces. If I run that, it actually updated CCA, which stands for capacitor cell array. But now let's look at the contents of that cell array. Oh, wait a minute. It only contains a single value. 100p that's simply the first capacitor value in the list there are three other capacitor values and there are two other entire rows and it just contains that first one okay why did that happen back to the command window now let's pick this command apart just a little bit we have a variable CCA equals and then we have C value which is the name of a cell array and then we have curly braces containing a colon that means extract all of the contents so that would be four character strings but then we hit a semicolon the semicolon says end of command now if it was in curly braces or if it was in parentheses or if it was in square brackets or something like that it would mean go to the next row but it's not Without being enclosed in something, it simply means this is the end of the command. So it's going to treat what follows after that as a new command. So first of all, there's one problem. Secondly, when we extract the contents, we've got four text strings. But we've only got one variable to put them in, so by default it only puts the first one of those character strings into that single variable. Now there are ways we could force that to accept all four of those, but written as it is, all it can do is accept the first of those character strings that was extracted. That's the problem with that. All right, let's look at a different variant. This time we're going to keep the outer curly braces but get rid of all of those inner curly braces. Okay, CCA has been updated. Let's look and see what it contains now. Oh, wait a minute. It now says it contains three cell arrays. Each cell, there are three of them in a stack, contains another cell array. The first one, of course, has... four text strings, the second one has four scalars, and so forth. But all of the information is there. We could display that if we wanted, but take my word for it at this point, all of the information is there. So what would happen if we did indeed try to write this information as it exists into our spreadsheet? Let's try it and see. Back to the command window, there is our command. Now note I am specifying sheet capacitors, so it will create a new sheet and label it capacitors and write the information into that sheet. Here we go. Whoa, we got an error. Now do take note of what the error says. Nested cell arrays are not supported. In other words, if you are trying to use right cell to write information. in a cell array that contains cell arrays is not going to work. So, okay, that one's off the table. Let's go back to our original form for creating that 3x4 cell array. Now, note the inner curly braces with the colons say, give me the contents of this. In this case, the first one, cvalue. is four text strings. Then we have a semicolon, which says go to next line. And then we have the four voltage ratings, then semicolon, next line, the number of each that we need. The whole thing is enclosed in curly braces, which says put all of this stuff into a cell array. So it does indeed put all of those individual items into a cell array. And again, if we look, there indeed is. all of our information one piece at a time, not sub-enclosed within cell arrays. Back to the command window. Now if we use the exact same write cell command, we don't get an error. Component list has been updated and if we look, we see that there is now a capacitor sheet and that sheet does indeed contain all of that information. In our next video, we will begin looking at how to read information in from a spreadsheet, modify it in MATLAB, and then write it back to an Excel workbook.