Download presentation
Presentation is loading. Please wait.
1
Day8– File-IO (Reduced-2007Dec)
ORG. Source: KC Huang, Chad Myers Chris Bristow, Greg Reeves Lecture 8, 5/3/06 Insert every few slides to have them mess around with stuff…
2
Writing to/Reading from text files
For any file I/O, you’ll always: Open a file: >> file_handle=fopen(‘your-filename’,’r’); >> file_handle=fopen(‘your-filename’,’w’); r read w write r+ read and write (don’t create) w+ truncate or create for read and write a open (or create if no file exists) and write <Read or write to the file> Close the file: >> fclose(file_handle);
3
Writing to text files Open a file: Write to the file using fprintf:
>> file_handle=fopen(‘your-filename’,’w’); w write (overwrites the file) a open (or create if no file exists) and write (appends to the file) Write to the file using fprintf: >> fprintf(file_handle,’formatted string %d %s’,var1,var2); Close the file: >> fclose(file_handle); To print to screen: fprintf(’formatted string %d %s’,var1,var2); To print to file: fprintf(file_handle,’formatted string %d %s’,var1,var2);
4
Writing to text files weekdays.txt file_io2.m
5
Reading from text files
Open a file: >> file_handle=fopen(‘your-filename’,’r’); Read from the file using fgetl, strread: while (1) current_line = fgetl(file_handle); (gets next line file,removes \n) if(~ischar(current_line)) break; (breaks out when file ends) end [num,day] = strread(current_line,’%d:\t%s’); (parses line) <do something with num and day here> Close the file: >> fclose(file_handle);
6
Reading from text files
file_io2.m
7
Reading from text files
For simple formats (single column, row, or tab-delimited), there is a built-in reading function: importdata For more sophisticated reading, see: textread,dlmread,textscan Another useful function for reading from text files: [next_token, remaining_str] = strtok(string,token)
8
Matlab scripts vs. functions
Two different kinds of .m files: Scripts Sets of related commands that don’t accept input arguments or return output arguments. Scripts can access/modify variables in the current workspace. Functions Self-contained sets of statements which accept input arguments and return output arguments (e.g. sin, abs, log). Each function has its own workspace consisting of what variables are passed to it and what variables it returns.
9
Matlab scripts expressions.m Example script: expressions.m Remember:
you need to be in the same directory to run a script from the command prompt.
10
Matlab functions mypolynomial.m Function file format:
separate .m file named <functionname>.m Inside the file: function [outvar1,outvar2,...] = <functionname>(invar1,invar2,...)
11
Matlab functions (2) mypolynomial_error.m
Variable scope demonstration: Will these work?
12
Matlab functions (3) mypolynomial_error.m Important points:
(1) functions can only use/modify variables local to them (3) Scripts can use/modify any variable in the workspace and any variables they create are put in the current workspace (2) scripts that call functions cannot access variables inside the function other than the inputs or returned value NOTE: Matlab’s functions are conceptually identical to Perl’s subroutines.
13
Structures Structures are just “data containers” that allow you to group related data together in a single object. Each object can have multiple “fields”, each of which is accessed using this “.” notation: structures.m gene .name ’p53’ .sequence ’aatcgtaggttaaa….’ .logratio 3.2
14
Creating/accessing structures
structures.m To initialize a structure, use a simple assignment: >> gene.name=‘p53’ To access a structure field, do just what you’d think: >> x = gene.logratio – 1.0
15
Multidimensional structures
Structures can also have several dimensions: structures.m gene(1,1) .name .sequence .logratio ’p53’ ’aatcgtaggttaaa….’ 3.2 gene(2,1) .name .sequence .logratio ’CG6445’ ’tccttagaga….’ -16.1 gene(2,1) .name .sequence .logratio ’CG6445’ ’tccttagaga….’ -16.1 gene(2,1) .name .sequence .logratio ’CG6445’ ’tccttagaga….’ -16.1 gene(n,1) .name .sequence .logratio ’CG6445’ ’tccttagaga….’ -16.1
16
Cell arrays Matlab is very strict about matrix dimensions: name = [‘John’; ‘Doe’] Matlab does not allow mixed data types in the same matrix: mymat = [‘John’,25] Basic idea: To allow arbitrary elements to be included in the same array, you must first put each one in a “cell”. Then, the cells can be put in a matrix. Perl allows you to create lists with elements of different types (strings, numbers, hashes, …). Matlab requires you to use a special kind of array: a cell array. [ ] = “cell” ‘John’ OR … ‘John’ ‘John’
17
Creating/accessing cell arrays:
To create a cell array, you must use curly braces: >> cellarray(1) = {’John’}; >> cellarray(2) = {24}; >> cellarray(3) = {[3 -10; 0.5 6]}; OR >> cellarray{1} = ’John’; >> cellarray{2} = 24; >> cellarray{3} = [3 -10; 0.5 6] >> cellarray = {’John’,24,[3 -10; 0.5 6]}; cell_examples.m cellarray = 'John' [24] [2x2 double] What happens if you use round parentheses? >> age2 = cellarray(2) age2 = [24] To access elements in a cell array, you also use curly braces: >> age = cellarray{2} >> cellarray{3}(2,1) age = 24 ans = 0.5000
18
Cell arrays of strings cell_examples.m
19
Numeric and mixed cell arrays
cell_examples.m
20
An aside: writing Java code in your Matlab scripts
Remember I said Matlab isn’t competitive with C++/Java and others in terms of efficiency? One way to get around this is to actually write/call Java code from within your Matlab scripts: my_hash = java.util.HashMap; my_hash.put(‘One’,1); my_hash.put(‘Two’,2); my_hash.get(‘One’)
21
Please fill out the course evaluation page at:
Questions?? Use Matlab help! Google… Stop by and see us Thanks for coming! Remember: Matlab lab 4:30-6pm tomorrow, Carl Icahn 200 or 280 Lab next week, Wed. 4-6, work on your own scripts with our help Please fill out the course evaluation page at:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.