MATLAB: Structures and Cell Arrays



Similar documents
MATLAB: Strings and File IO

The University of Chicago Booth School of Business Tools for Business Analysis: Excel and Matlab Winter, 2012.

Beginner s Matlab Tutorial

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

The FTS Interactive Trader lets you create program trading strategies, as follows:

GUI Input and Output. Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University

VBA and Databases (see Chapter 14 )

Excel 2003, MS Access 2003, FileMaker Pro 8. Which One Should I Use?

Connecting to an Excel Workbook with ADO

Introduction. Chapter 1

PIC 10A. Lecture 7: Graphics II and intro to the if statement

STEP TWO: Highlight the data set, then select DATA PIVOT TABLE

RIT Installation Instructions

Importing Data from a Dat or Text File into SPSS

Relational Database Basics Review

Tutorial Program. 1. Basics

18.06 Problem Set 4 Solution Due Wednesday, 11 March 2009 at 4 pm in Total: 175 points.

The FTS Real Time System lets you create algorithmic trading strategies, as follows:

Appendix: Tutorial Introduction to MATLAB

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

USC Marshall School of Business Marshall Information Services

Financial Data Access with SQL, Excel & VBA

MS Excel. Handout: Level 2. elearning Department. Copyright 2016 CMS e-learning Department. All Rights Reserved. Page 1 of 11

Microsoft' Excel & Access Integration

Creating a Gradebook in Excel

Spreadsheet Model for Student Evaluation and Grading

EXCEL VBA ( MACRO PROGRAMMING ) LEVEL SEPTEMBER AM-5.00PM MENARA PJ@AMCORP PETALING JAYA

Introduction to Microsoft Access 2003

Introduction to Matlab

Working with Macros and VBA in Excel 2007

SPSS: Getting Started. For Windows

Microsoft Access 2010

DATA HANDLING: SETTING UP A DATABASE

Database File. Table. Field. Datatype. Value. Department of Computer and Mathematical Sciences

A Brief Introduction to MySQL

SPSS Workbook 1 Data Entry : Questionnaire Data

(!' ) "' # "*# "!(!' +,

Module 9 Ad Hoc Queries

VBA PROGRAMMING FOR EXCEL FREDRIC B. GLUCK

LabVIEW Report Generation Toolkit for Microsoft Office

Database Forms and Reports Tutorial

jexcel plugin user manual v0.2

A Comparison of SAS versus Microsoft Excel and Access s Inbuilt VBA Functionality

Introduction to Modern Data Acquisition with LabVIEW and MATLAB. By Matt Hollingsworth

Contract Management Submittal Log Import Importing the Submittal Items From MS Excel

VLOOKUP Functions How do I?

FrontStream CRM Import Guide Page 2

SPSS for Windows importing and exporting data

Instructions for Using Excel as a Grade Book

SMART Sync Windows operating systems. System administrator s guide

Microsoft Access Glossary of Terms

Introduction to Python

Oracle Database 10g Express

A basic create statement for a simple student table would look like the following.

Introduction to R Statistical Software

Siemens Applied Automation Page 1 11/26/03 9:57 PM. Maxum ODBC 3.11

CS 1133, LAB 2: FUNCTIONS AND TESTING

ODBC (Open Database Connectivity) for MS-Excel Microsoft OLE DB Provider for ODBC Drivers (Legitronic v3.6.2 & Later)

3.2 Matrix Multiplication

Once the schema has been designed, it can be implemented in the RDBMS.

Getting Ready for ICD-10

INSTALLING, CONFIGURING, AND DEVELOPING WITH XAMPP

Beyond the Mouse A Short Course on Programming

Build standalone executables from MATLAB code

Canisius College Computer Science Department Computer Programming for Science CSC107 & CSC107L Fall 2014

Tutorial on Operations on Database using JDeveloper

TeleTrader Toolbox for MATLAB

1. Starting the management of a subscribers list with emill

How To Read Data Files With Spss For Free On Windows (Spss)

Using D2L Brightspace for the First Time

IBM SPSS Statistics 20 Part 4: Chi-Square and ANOVA

Directions for the Well Allocation Deck Upload spreadsheet

Getting Started. Tutorial RIT API

1 Organizing time series in Matlab structures

SAPScript. A Standard Text is a like our normal documents. In Standard Text, you can create standard documents like letters, articles etc

Dynamics GP Insights to Manufacturing

XMailer Reference Guide

ing a large amount of recipients

Chapter 24: Creating Reports and Extracting Data

Mail Merge Tutorial (for Word ) By Allison King Spring 2007 (updated Fall 2007)

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VI

You are building a learning programme on Dokeos LMS and now has come the time to import users in the system. We will address this need at 3 levels

LabVIEW Day 6: Saving Files and Making Sub vis

THE HELLO WORLD PROJECT

Time Clock Import Setup & Use

Almost all spreadsheet programs are based on a simple concept: the malleable matrix.

Introduction to Microsoft Access

Myridas Catalogue Based Sales User Guide

NEXT-ANALYTICS lets you specify more than one profile View in a single query.

Opening a Database in Avery DesignPro 4.0 using ODBC

Transcription:

MATLAB: Structures and Cell Arrays Kipp Martin University of Chicago Booth School of Business February 16, 2012

The M-files The following files are used in this lecture. studentstruct.mat stockdata.mat readstudents.m studentscores.txt createcellarray.m studentrecordcell.mat

Outline Structures Cell Arrays

4 In MATLAB everything is an array. Problem 1: You need mixed data types as in an Excel range.

In MATLAB you cannot create an array that is a direct analog to an Excel range which can contain mixed data types. In MATLAB you cannot do the following: >> x = [ hello 55 ; world 77] x = hello7 worldm Poor MATLAB gets very confused when you try to mix data types in a single array.

There are excellent alternatives to this problem: Use a MATLAB structure. A structure is like a class. Each property of the structure is an array. The arrays may be of a different data type. Use a MATLAB cell array. In a cell array, each element of the array is another array. We are generating arrays recursively and they do not need to be of the same data type. First we work with structures.

In order to create the equivalent of an Excel range in MATLAB you would create a MATLAB structure. Then you would create an array, where each element of the array is a structure. A structure is similar to a class in VBA except it does not have methods. A MATLAB structure only has properties. In our example of students and their grades, there will be structure for each student (row). There will be property for each column. For example, there will be a LastName, FirstName, Midterm, etc. property.

Consider the first student. I define the structure (which I name studentstruct) for Tom Jones as follows: studentstruct(1).firstname = Tom studentstruct(1).lastname = Jones studentstruct(1).program = Evening MBA studentstruct(1).section = 81 studentstruct(1).midterm = 63... studentstruct(1).quiz5 = 56

Likewise for student number 4, Kathy Murigami, I have: studentstruct(4).firstname = Kathy studentstruct(4).lastname = Murigami studentstruct(4).program = Evening MBA studentstruct(4).section = 81 studentstruct(4).midterm = 96... studentstruct(4).quiz5 = 66

Here is another way I could have created the structure associate with Kathy Murigami using the struct function. studentstruct(4) = struct( FirstName, Kathy,... LastName, Murigami... Program, Evening MBA... Section, 81... Midterm 96......) In this case we are using name-value pairs.

Problem 2: In a MATLAB array every row must have the same number of columns (or each column the same number of rows). You cannot have: A = [1 2 3; 4 5] This is very limiting. We want to store data where parts of the data are not only of a different type, but a different size. Consider the stock data in the file portoptdata.xlsx

12 How might we represent this as a structure?

See stockdata.mat Property yearnames stocknames stockreturn S&PReturns Probabilities MinReturnLowerBound MInReturnUpperBound Data Type structure structure matrix (double) array (double) array (double) double double

The structure stockdata in the variable editor. Why did I store yearnames and stocknames as structures rather than arrays.

15 For the student scores data see: studentstruct.mat a MATLAB structure with the student grades studentscores.txt text file with comma separated values, this is used to generate studentstruct readstudents.m the MATLAB m-file that reads the data in studentscores.txt and generates studentstruct For the stock data data see: stockdata.mat a MATLAB structure with the magazine subscription data hw 5.xlsm the structure stockdata.mat is modeled after the spreadsheet stockdata is this workbook.

Creating a structure: How do we create a structure? How did I create stockdata.mat? In MATLAB there is no Dim command. There is no VBA equivalent of Dim stockdata As Structure We have three options in MATLAB.

Creating a structure Option 1: Just start typing away in the command window! Let s create a simple structure names that will have two properties: firstname and lastname. names(1).firstname = Joey names(1).lastname = Votto names(2).firstname = Aroldis names(2).lastname = Chapman we use 1-based counting records don t need to be complete e.g. we can have a firstname but no lastname

Creating a structure Option 2: Use the struct() method in MATLAB. Create a 1 by 2 structure name with properties firstname and lastname name(1, 2) = struct( firstname, [], lastname, []) Create a 2 by 1 structure name with properties firstname and lastname name(2, 1) = struct( firstname, [], lastname, []) You can add a third, fourth, etc., record at any time.

Creating a structure Option 3: Read a file (or some other data source text file, XML file, SQL database) and use MATLAB scripting. We illustrate the above with createstudentsstruct.m in the next module.

After creating a structure in MATLAB use save, e.g. save stockdata This will save the structure with a.mat extension. To get the structure back: load stockdata double click on stockdata.mat in explorer window Caution: the above will save any other variables you have in your work space.

21 If you have other variables around and want to save only the structure, do the following: save FILENAME STRUCTURENAME save tmp stockdata will save the structure stockdata in the.mat file tmp.mat. The practice I like is: save stockdata stockdata This way I have a.mat file with the same name as my structure.

The size() function is very useful when working with structures. What is size(stockdata)? What is size(stockdata.stocknames)? What is size(stockdata(1).stocknames)? What is size(stockdata.stocknames(1))? What is size(stockdata.stocknames(1).name)? What is size(stockdata.stockreturns)? What is size(stockdata(1,1).stocknames)

Addressing: What you learned about addressing for matrices of numbers and strings pretty much applies to structures. What is stockdata.stockreturns(5,3)? What is stockdata.stockreturns(5,:)? What is stockdata.stockreturns(5,[1 4])? What is stockdata.yearnames(1,[1 4])? be careful What is stockdata.yearnames(1).name(1,[1 4])?

Structures versus string data: What is the size of the following A-matrix? A = strvcat( a, abcdefghijklmnopqrstuvwxyz ) What about the following? A(1).string = a A(2).string= abcdefghijklmnopqrstuvwxyz Advantages/disadvantages of the two approaches?

Cell Array 25 Instead of >> x = [ hello 55 ; world 77] we write >> x = { hello 55 ; world 77} and we get x = hello [55] world [77] We have a created a 2 2 cell array.

Cell Array It is even neater. Watch this: >> A = [1 3; 7.1 9] A = 1.0000 3.0000 7.1000 9.0000 >> x = { hello 55; world A} x = hello [ 55] world [2x2 double]

Cell Array Here is what we have: Cell (1,1) is a string array with with five characters Cell (1,2) is a 1 1 double array Cell (2,1) is a string array with with five characters Cell (2,2) is a 2 2 double array

Cell Array 28 Addressing cell arrays: x{1,1} referes to the string hello x{2,2} referes to the 2 double array [1 3; 7.1 9] What does x{2,2}(2, 1) refer to? What does x{2,1}(3) refer to? What does x{2,1}(1, 2) refer to?

Cell Array See the m-file createcellarray.m. This takes the file studentscores.txt and creates a cell array studentrecordcell.