870 likes | 883 Views
Day 4 review. TABLES FUNCTIONS. Let's fire up Matlab. TODAY. Saving in Matlab formatted data (= fprintf ) Matrices as images (maybe other types of variables). What is in memory?. FILES with DATA: RTs… MATLAB matrices. FILES with TEXT: list of words later on: images and sounds.
E N D
Day 4 review • TABLES • FUNCTIONS. • Let's fire up Matlab.
TODAY Saving in Matlab formatted data (=fprintf) Matrices as images (maybe other types of variables)
What is in memory? • FILES with DATA: RTs… • MATLAB matrices. • FILES with TEXT: list of words • later on: images and sounds.
SAVING AND READING MATRICES • Use the command 'save' to save a matrix in matlab format (.mat), which is ONLY readable with Matlab. • type: • myworld = 'is beautiful'; • yourworld = 0; • save Worlds;
SAVING AND READING MATRICES • save NAME • saves ALL the variables in your workplace, regardless of differences in format, on file NAME.mat • Type: > clear all %CHECK WORKSPACE %CHECK CURRENT DIR. > load Worlds
SAVING AND READING MATRICES • save NAME var1 var2 • specifies which variables to save in file NAME.mat • if you want to ADD stuff to a current mat file: • save NAME var1 –append %here, var1 is being added to your % MAT file named NAME
SAVING AND READING MATRICES • wilcard * • save avariables a* %any guesses? • saves all variables starting with a in file avariables.mat
SAVING AND READING MATRICES • load name loads all variables in name.mat. • load name var1 var2 • only loads var1 var2
READING AND WRITING FILES p.18 • so, you might need to access a file and pull information from it, or print output/data from calculations you've performed. • print to file: fprintf • read from file: fscanf
let's do 'f'iles… • files need to be given a file identifier (to keep track of them) and need to be OPENED. > fid = fopen(filename) fid is file identifier (1, 2, 3..) -1 if file not found. FILE has to be in working directory or in PATH.
Open goldilocks.txt • fid = fopen('goldilocks.txt') • what happens? • Note you can call fid whatever you want… • Close goldilocks.txt: • fclose(fid) %0 means success. • OPEN IT AGAIN both in matlab and NOTEPAD.
What can you do with an open file? • get first line of text: • TRY: • firstline = fgetl(fid) • %don't suppress output. • Run the command again. • Run the command again. • Run the command again. What's going on?
What can you do with an open file? • NOW TRY • nextline = fgets(fid) • ANY DIFFERENCES?
At the end… • of lines, there is a new line symbol ( \n) invisible to mere humans • fgetl reads a line but does NOT copy the end of line character to the string. fgets does.
At the end… • of files, there is an end of file character, which we test with: • feof(fid) : 0 while not end of file 1 once it is found.
Exercise: • PRINT TO THE SCREEN THE ENTIRE STORY OF GOLDILOCKS. • 'while' might come in handy.
Exercise: • fid = fopen('goldilocks.txt'); • while feof(fid) ==0 newline = fgetl(fid) end fclose(fid);
Exercise > fid = fopen('goldilocks.txt'); > firstline = fgetl(fid); FIND SPACE CHARACTERS IN FIRST LINE. > spaces = find(firstline == ' ') %Double check with your array editor
FPRINTF: weird. p16-17 • fprintf allows us to write to a file. • fprintf(fileID,'PRINTING FORMAT', variable). The 'printing format' is where you manage your CURSOR.
FPRINTF: weird. p16-17 • OPEN PAGE 17. • flags page 17 • Field width: MINIMUM number of digits (spaces) to be printed • Precision: decimals after period. • CURSOR: RESERVES Leftward space Overruns rightwards.
FPRINTF: weird. p16-17 • Most common conversion characters. • %c single character • %d decimal notation • %s string of characters • INSIDE PRINTING area: \n new line \t horizontal tab
WRITING AND READING FORMATTED DATA • DATA files are formatted. We can write formatted data with fprintf and read them into variables with fscanf. • It's all about cursor placement.
The cursor: • DON'T SPECIFY FID: (works same) prints to command windown. • fprintf('I print this.\n'); • fprintf('I print this.'); • Now, let's work on cursor placement.
The cursor: fprintf('I print this.') %shift enter fprintf('and this.'); -->cursor continues printing where it left off. --> Add a space inside the '' at end of first fprintf command.
The cursor: RESERVING LEFTWARDS SPACE: • myname='alejo'; • fprintf('%s%s%s', myname, myname, myname); %compare to • fprintf('%6s%6s%6s', myname, myname, myname); • THERE IS ONE UNUSED "LEFT" SPACE
The cursor: FORMATTED DATA • trial=1:10; • condition = mod(trial,2); • fprintf('%2d %d',trial, condition); • fprintf('%2d %d\n',trial, condition); • What happened?
The cursor: FORMATTED DATA • for count=1:10 fprintf('%2d %d\n',trial(count),… condition(count)); end; • What happened?
Intermixing text and variables • fprintf('This is trial %2d.\n', trial); • for count=1:10 • fprintf('This is trial %2d, and condition %d\n.',trial(count), condition(count)); end;
Exercise • Create a three column matrix with: • first column: numbers from 1-10. • second column: alternating 0-1. • third column: random number between 150 and 1000. • WRITE TO screen: • think trial number, condition, RT.
Solution (1) • data = zeros(10,3); • data(:,1)=1:10; • data(:,2)=mod(data(:,1),2); • data(:,3)=rand(1,10)*850 +150;
Now, let's print it! • with for loop: • for count=1:10 • fprintf('%2d %d %3.1f\n',… data(count,1),… data(count,2),data(count,3)); end;
Exercise (2) • WITHOUT FOR LOOPcompare: data %(enter) and fprintf('%2d %d %3.1f\n',data); • writes data column-wise. • Treats matrix as comma-delimited list. • CONTINUES EXECUTION until all the specified variables HAVE BEEN PRINTED. • what we want is: data': the transposition of data
Last issue. • How do you print a ' or % or \ with fprintf?ex: it's a beautiful day!ex: I'm 100% certain 2\4=2. • Answer: you double the escape character to make it printable (page17) > fprintf('I''m 100%% certain 2\\4=2.')
IMAGES (1) The philosophy: Any two dimensional matrix can be easily transformed into an image. If matrix is 40 x 40 --> 40 x 40 picture. matrix(i,j) --> pixel(y,x) where y=ith pixel on y scaleand x=jth pixel on x scale
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1] elements of br: br(2,3) = 1
IMAGES (1) x axis Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1] y axis elements of br: br(2,3) = 1
IMAGES (1) x axis Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1] y axis elements of br: br(2,3) = 1 br(2,3) has coordinates x=3 y=2
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1] 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1] say… 0 = black 1 = red 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1
IMAGES (1) p. 22 • Let's play with Matlab's demo: • Type: > clear all > load durer %check workspace. > image(X) %what happened? > colormap(map) %ruminate on this… > axis equal > axis image %same as equal, but image fits tightly > axis off %turns off tick marks
IMAGES (1) p. 22 • Try: > X(1,1) > max(max(X)) %returns largest value in matrix > min(min(X)) %returns smallest value in matrix • so we have values between 1 and 128, each translated into a color through a colormap (aka Color Look Up Table). • what are the dimensions of the variable map?
COLORMAPS p. 22 “A colormap consists of a set of entries defining color values. The colormap associated with a window is used to display the contents of the window; each pixel value indexes the colormap to produce an RGB value that drives the guns of a monitor. Depending on hardware limitations, one or more colormaps can be installed at one time so that windows associated with those maps display with true colors.”
COLORMAPS p. 22 RGB value [200 0 0]
COLORMAPS p. 22 = SHORTCUTS to your 'kinda colors'. How many colors in my computer? - 8 bit color = 256 colors. (GIFs) - 24 bit color = 8 bits/gun. 256^3 = 16.7 million - 36 bit color = 12 bits/gun. (2^12)^3 = 68 billion. Most today are: 32 bit: 8 bits per gun, plus 8 bits of transparency…
CLUTs Matlab native colormaps (you can make your own): • hsv - Hue-saturation-value color map. • hot - Black-red-yellow-white color map. • gray - Linear gray-scale color map. • bone - Gray-scale with tinge of blue color map. • copper - Linear copper-tone color map. • pink - Pastel shades of pink color map. • white - All white color map. • flag - Alternating red, white, blue, and black color map. • lines - Color map with the line colors. • colorcube - Enhanced color-cube color map. • vga - Windows colormap for 16 colors. • jet - Variant of HSV. • prism - Prism color map. • cool - Shades of cyan and magenta color map. • autumn - Shades of red and yellow color map. • spring - Shades of magenta and yellow color map. • winter - Shades of blue and green color map. • summer - Shades of green and yellow color map.
Exercise Look at map. What do you notice? Display Durer's etching in all shades of red. >map(:,2)=0; >map(:,3)=0; >colormap(map)