![]() There is a lot of Matlab left to learn, and unfortunately, it's sometimes needed for very basic things.Again, we use logical indexing with strtype to select the string values in raw, convert them to numbers, and assign these to the correct subset of dat. Here, we do have a nice, vectorized function, str2double that converts all our string-valued cells to double-precision numbers (this won't be quite right for your integers, since it will add decimal points, but you can fix that later). ![]() Finally, we need to convert the string cells to number and copy them over.We can convert those to an array using the cell2mat function. On the right-hand side, we need to get cells from raw using ~strtype, but that returns cells. We use logical indexing, with ~strtype as our logical (it's 0 for strings and 1 for numbers). Next, we want to copy all the numbers.For reasons we'll cover in the intermediate class, you need to do that using The return from cellfun is then a matrix the same size as the cell array, with a 1 if the corresponding cell contains a string and 0 otherwise. To do that, we need to supply the name of a function to use on each cell. cellfun is a function that applies a function to every element of a cell array. As you will find, functions like ischar that test whether a single variable is a string, don't work so well on cell array. To do this, we need to know which cells contain numbers.First, we copy over everything that's already a number, then we convert the rest to numbers and copy them the same way. Our basic plan is to copy all the data over to a new array in two pieces.This would be really painful to do with a loop, but if you know how to do it with a loop, and that's the only way you know how to do it, then use a loop! Go for a solution that works before you waste to much time on being elegant.It took me several minutes to work out how it had to be done. Why do we solve the problem like this? Or better yet: what should our thought process be when we approach such a problem? Some thoughts: Strtype = %apply the ischar function to every element of rawĭat(~strtype) = cell2mat(raw(~strtype)) %copy over cells that are already numbersĭat(strtype) = str2double(raw(strtype)) %convert non-numeric cells to numbers, then copy If we used the xlsread option, we can do some data cleaning (advanced): dat=nan(size(raw)) %make an empty cell array Both are indexed from 0, so that the arguments 1 and 0 above mean to start at the second row, first column. The two extra arguments supplied are the row and column at which to start. csvread always returns a numerical matrix.In raw, depending on whether or not you have stray spaces, Matlab may convert your numbers to strings inside the cell array. xlsread returns three arguments, the numerical data, num (in an array), the text data, txt (in a cell array the same size as the spreadsheet, with all numerical data removed), and the "raw" data in a cell array ( raw), where each cell array is a cell in the spreadsheet.In its simplest form, we want to use one of the following commands to pull data = xlsread( 'jury_data.xlsx') Those of you with a spreadsheet program installed may want to take a look at the data. Now move to the directory where the file is located. (Excel will read and save files in this format, and it's actually human-readable, since each line is just the entries in each row, separated by a delimeter: commas, spaces, tabs, etc.) The data are currently stored in a comma-separated value file (csv), which is a pretty typical and portable data format. ![]() These are data extracted from the Multnomah County Jury Project, which tabulated jury decisions in Multnomah County, Oregon, in the period 1973-1976. Getting Startedįirst you'll need to download the data ( xls xlsx csv). You'll have to play around to figure out which is best for you. (I had to clean the data a lot to get this working.)Īs a result, I'm going to list several options below.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |