12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

whosName Size Bytes Classt 4x1 658 cell arrayGr<strong>and</strong> total is 149 elements using 658 bytes>> t(1)ans =’O sacred receptacle <strong>of</strong> my joys,’>> t{1}ans =O sacred receptacle <strong>of</strong> my joys,>> t{1}(1)ans =O>> t{1}(1:8)ans =O sacredLet us add another element to the cell array by putting a 3 × 3 matrixin the first row <strong>of</strong> the second column:>> t{1,2} = spiral(3)t =[1x31 char] [3x3 double][1x34 char] [][1x41 char] [][1x39 char] []matlab has filled the rest <strong>of</strong> the cells in column 2 with empty cells. Weused the curly brackets t{1,2} to refer to that particular cell. If we hadused ordinary round brackets, we would have produced an error:>> t(1,2) = spiral(3)??? Conversion to cell from double is not possible.This is because there is a difference between indexing cells <strong>and</strong> indexingtheir contents. For example, to extract the word “virtue” from the secondline <strong>of</strong> the quotation in the first column, we need to access the cell {2,1},then get characters 15 to 20 from that cell’s contents:>> t{2,1}(15:20)ans =virtueWhen assigning a cell you can use the curly brackets on either the left orright h<strong>and</strong> side <strong>of</strong> the equals sign, but you must put them somewhere, totell matlab that you want this to be a cell. Otherwise, matlab thinksyou are defining a mathematical matrix <strong>and</strong> gives you an error to theeffect that the things on each side <strong>of</strong> the equal sign have different sizes.For example, we can type:c○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!