1 / 87

Day 4 review

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.

taracoleman
Download Presentation

Day 4 review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Day 4 review • TABLES • FUNCTIONS. • Let's fire up Matlab.

  2. TODAY Saving in Matlab formatted data (=fprintf) Matrices as images (maybe other types of variables)

  3. What is in memory? • FILES with DATA: RTs… • MATLAB matrices. • FILES with TEXT: list of words • later on: images and sounds.

  4. 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;

  5. 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

  6. 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

  7. SAVING AND READING MATRICES • wilcard * • save avariables a* %any guesses? • saves all variables starting with a in file avariables.mat

  8. SAVING AND READING MATRICES • load name loads all variables in name.mat. • load name var1 var2 • only loads var1 var2

  9. 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

  10. 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.

  11. 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.

  12. 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?

  13. What can you do with an open file? • NOW TRY • nextline = fgets(fid) • ANY DIFFERENCES?

  14. 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.

  15. 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.

  16. Exercise: • PRINT TO THE SCREEN THE ENTIRE STORY OF GOLDILOCKS. • 'while' might come in handy.

  17. Exercise: • fid = fopen('goldilocks.txt'); • while feof(fid) ==0 newline = fgetl(fid) end fclose(fid);

  18. Exercise > fid = fopen('goldilocks.txt'); > firstline = fgetl(fid); FIND SPACE CHARACTERS IN FIRST LINE. > spaces = find(firstline == ' ') %Double check with your array editor

  19. 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.

  20. 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.

  21. 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

  22. 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.

  23. 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.

  24. 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.

  25. 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

  26. The cursor: FORMATTED DATA • trial=1:10; • condition = mod(trial,2); • fprintf('%2d %d',trial, condition); • fprintf('%2d %d\n',trial, condition); • What happened?

  27. The cursor: FORMATTED DATA • for count=1:10 fprintf('%2d %d\n',trial(count),… condition(count)); end; • What happened?

  28. 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;

  29. 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.

  30. Solution (1) • data = zeros(10,3); • data(:,1)=1:10; • data(:,2)=mod(data(:,1),2); • data(:,3)=rand(1,10)*850 +150;

  31. 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;

  32. 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

  33. 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.')

  34. Questions?

  35. 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

  36. IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]

  37. IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]

  38. 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

  39. 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

  40. 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

  41. IMAGES (1) Example: br = [ 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1]

  42. 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

  43. 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

  44. 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

  45. 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?

  46. 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.”

  47. COLORMAPS p. 22 RGB value [200 0 0]

  48. 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…

  49. 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.

  50. 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)

More Related