Lesson 2: Basic Output You’ve got data……now what???

Slides:



Advertisements
Similar presentations
COMP234 Perl Printing Special Quotes File Handling.
Advertisements

Matlab DIY Lesson 3: Parsing data. Today's Lesson “ parse ” = analyze into its parts o Sort the data you want MATLAB to use o Designate different segments.
Fprintf and other examples. Save command >> !ls >> a = 3; >> save afile a >> !ls afile.mat >> !dir Directory of C:\MATLAB6p5\work\save 11/21/ :30.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
EGR 106 – Truss Design Project (cont.) Truss design programs Graphical interface tools in Matlab Saving and loading data Formatted output Project Assignment.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
General Computer Science for Engineers CISC 106 Lecture 07 Dr. John Cavazos Computer and Information Sciences 2/25/2009.
Matlab DIY Lesson 1: Reading Data. Purpose of this Seminar Basic Ability to handle Data Analysis and Presentation in Matlab Understand how data is organized.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Lesson 2: Basic Output You’ve got data……now what???
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Input/Output Functions Selim Aksoy Bilkent University Department of Computer Engineering
The textread Function It is designed to read ASCII files that are formatted into columns of data Each column can be of a different type It is useful for.
Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH)
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
AN ENGINEER’S GUIDE TO MATLAB
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
File Handling Spring 2013Programming and Data Structure1.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
File IO and command line input CSE 2451 Rong Shi.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)
IDL Tutorial Day 1 Goals: 1) Introduce IDL basics 2) Describe fundamental IDL structures Angela Des Jardins
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
5 1 Data Files CGI/Perl Programming By Diane Zak.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Chapter 2 part #1 C++ Program Structure
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Input Output Garbage In, Garbage Out. Outline Announcements: –HWII solutions on web soon –Homework III: due Wednesday Advanced ASCII Binary Basics Cell-arrays.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Input Output IO and datafiles Credits also to Prof Selim Aksoy (Bilkent University.
Working with files Saving and loading Matlab variables to and from .mat files does not require any special file handling, just use save() and load() However,
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
File Access (7.5) CSE 2031 Fall July 2018.
Programming in C Input / Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Advanced Data Import & Export Jeff Henrikson
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Note on Indexing of Array Elements
Input/Output Functions
Module 12 Input and Output
Input/Output Functions
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Files Chapter 8.
Presentation transcript:

Lesson 2: Basic Output You’ve got data……now what???

Purpose of lesson 2 Creating input and output pathways Designating an input file Creating an output file

Review from lesson 1 What is a: o Integer? o Double? o Character? o String? o Array? o Cell?

First Step - Writing your first code Using the Matlab Editor Comments %This is a comment This is code Testing code in the Matlab Command Window Declaring a variable x = 1;

Opening a file from Matlab What you need: Name of the File Path to the File Filename: data.csv collection.txt subject117.tsv File Path: On Windows: C:\My Documents\Collection\ On Mac/Unix: /Users/default/Collection/

Declaring File Name and Path %Name of the File infilename = 'L2_data.csv'; %Path to File inpath = 'C:\Documents and Settings\Sandy\Desktop\Classwork\' ;

Inpath is a variable that describes the location of the data Inpath = ['/Desktop/classwork/',subj_num,'/']; Declaring a variable String associated with location of file Previously declared variable Folder designation? [ ] indicates that you have created a new string with everything included

Let’s open the file %Name of the File infilename = 'L2_data.csv'; %Path to File inpath = 'C:\Desktop\Classwork\' ; %Open the file for editing r = csvread([infilename,inpath],1,0);

Were you successful? Try again %adjust so you only look at columns with numbers r = csvread([infilename,inpath],1,1); (in lesson 3 we will show you textscan so you can read columns with strings in them

Preparing an Output Location % Outpath needs to exist, not necessarily outfilename outpath='/Desktop/classwork/L2_data/'; outfilename=[date,'firstoutput.csv'];

Error protection Tiny checks to protect against error. %Check to make sure outpath exists if exist(outpath)==0 mkdir(outpath); end

Preparing for Output %Open [outpath,outfile] for writing out_fid = fopen([outpath,outfilename], 'w+');

Wow! That was a lot of new stuff all at once, wasn't it?

Opening a file for output out_fid = fopen([outpath,outfilename], 'w+'); fid is a scalar MATLAB® integer, called a file identifier. You use the fid as the first argument to other file input/output routines. Function “open” String showing location of file File permission

Array Concatenation: [A,B] (what can you put into a string?) Concatenation: The act of putting two things together. [A,B] = Concatenation A and B into an Array Try: w = [1,2] x = ['Subject','Condition'] y = ['RT: ', 223] z = ['Perturb',true]

'w+'? fopen permissions that you care about: (if in doubt look these up by searching MATLAB help) “help fopen” r = Open a file for reading. w = Open or create a file for writing, discard contents. a = Open or create a file and append data to it. Update mode (+) allows simultaneous reading and writing.

Printing Data to a File %Outputting just a String, useful for Headers. fprintf(out_fid,'subj_num,group,AP_RMS,\n'); MATLAB function which specifies writing information to a “device” Specifies the object (location) for writing the information Gives the string that is to be written Commas indicate new columns \n indicates a new row

Printing Data to a File %Outputting just a String, useful for Headers. fprintf(out_fid,'subj_num,group,AP_RMS,\n'); %Outputting data from variables. fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); Format specifier for how information is to be written, s= string d= decimal f=fixed point decimal Look these up by typing “ help fprintf” Values to go in spaces

Printing Data to a File %Outputting data from variables. fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); Declare your variables subj_num=‘a1’; Group=‘3’; AP=r(:,2); AP_RMS=std(AP);

'\n'? Special Characters you will likely use: \n = The newline character \t = The tab character \\ = The backslash character fprintf(out_fid, '%s,%s,%s\n', subj_num,group,AP_RMS,); fprintf(out_fid, '%s\t%s\t%s\n', subj_num,group,AP_RMS,);

Output per data set One line vs. multiple lines fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS,); fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS(i),); fprintf(out_fid, '%s,%s,/n', subj_num,AP_RMS(1,1),); Single variable output variable for current row (i) variable from row 1, column 1 of the AP_RMS array

Practice opening files Open file from class or open one of your own data files Keep records of the error messages Bring a record of errors to class Bring solution if you found it IF you did NOT find solution please send the error to Wayne and Sandy before 10 am Monday morning.

Practice making errors Counting is critical for output files Play with some common things that create weirdness in your output file 1) More headers than variables 2) Less headers than variables 3) More % s (or d or f) than variables 4) Put less %s than variables 5) Take the /n off the end of fprintf on data file