%% 1. Open a new figure and run the first two lines of code below. % Then, use the set function to set the x and y axes to go from -1 to +1. % Next, open a new figure, and run the second line before the first line. % What have you learned about the function line? Finally, change the code % to make the lines from both functions magenta. Setting line colors in the % line function is not the same as in the plot function. plot([0 cos(pi/3)],[0 sin(pi/3)]) line([0 cos(pi/4)],[0 sin(pi/4)]) %% 2. Run the code 'niceplot.m' (Folder Dados) % Go through all the code and try to understand what each line is modifying in the chart. % based on this code write a new one that plots the same data but with colors or other specs of your choice % (e.g. colors, marker, markercolor, Ticks, etc). %% 3. Open a new figure and run the first two lines of code below. % Then, use the set function to set the x and y axes to go from ?1 to +1. % Next, open a new figure, and run the second line before the first line. % What have you learned about the function line? Finally, change the code % to make the lines from both functions magenta. Setting line colors in the % line function is not the same as in the plot function. plot([0 cos(pi/3)],[0 sin(pi/3)]) line([0 cos(pi/4)],[0 sin(pi/4)]) %% 4. How would you use the set function to specify that the y-axis % limits should be the same as the x-axis limits? %% 5. There is a MATLAB function called csvread, which reads in % comma separated values (CSV)-formatted files. % The online code includes a file called somedata.csv. % Based on the result of help csvread, figure out how to import the data. % Next, open the file using a non-MATLAB text editor such as Excel or Notepad, and confirm % that MATLAB imported the data correctly (e.g., what happens to missing entries in the CSV file?). % Finally, generate a two-dimensional matrix of uniformly distributed random % numbers between 10 and 35, and export this matrix to a file using csvwrite. %% %% 6. Write a code to read all the "data_rat.mat" files in the folder "Dados" % Use the uigetdir function. Coment every line of the code you produced % explaining what the commands are doing. %% 7. Inspect the following two lines of code (variable var4fname is a string % variable that contains the name of the output file). % Neither contains an error, but one will do something other than the intended action. Which one is it and why? save(var4fname,'var1','varB') save('var4fname','var1','varB') %% 8. Images % Image data are shown very often in neuroscience. % Not only pictures of imaging (fMRI and optical imaging) but also many complex % results in, for example, electrophysiology are often best viewed as images. % An image is a matrix of numbers, and the value at each pixel can be mapped % onto a color and shown in a picture. % There are several functions you can use to show images in MATLAB, one of which is called imagesc. pic = imread('saturn.png'); %What the function imread does? figure subplot(131) image(pic) subplot(132) imagesc(pic) subplot(133) imshow(pic) % I) What is the difference between image, imagesc and imshow? % The image is 1,500 pixels wide and 1,200 pixels high. % The third dimension contains the red, green, and blue channels. % The function imagesc can take a three-dimensional matrix as input, but the % third dimension must have exactly three elements. Watch what happens when we try % to make an image from a matrix with an oversized third dimension. pic2 = pic; pic2(:,:,4) = pic(:,:,1); imagesc(pic2) % II) What happened? % The function imagesc also handles two-dimensional matrices; % in this case, the matrix element values signify pixel intensity, % and the figure's color legend maps those intensity values onto color or grayscale values. % Let's look at each of Saturn's color channels: colorchans = { 'red';'green';'blue' }; for chani=1:3 subplot(2,2,chani) imagesc(pic(:,:,chani)) axis off set(gca,'clim',[0 255]) title([ colorchans{chani} ' channel' ]) end % III) the function imagesc is very usefull to plot values of two dimensional matrix. % For example it is very used for plotting EEG data where we have % information about frequency bins, time bins and the power of each bin load('spectrogram_example.mat') % The above file contains information from an experiment performed by Danilo % Bennete Marques: the power (after time-frequency % decompostion; don't worry about this!) from the Hippocampus EEG during a % task. Time '0' indicates when the cue (light goes on) start. imagesc(times,freqs,spectrogram_example) axis xy colorbar xlabel('Time (s)') ylabel('Frequency (Hz)') % Can you explain what the graphic is ploting? % IV) change the colormap to jet, limit the frequencies to 1.5 to 50 Hz, % and the colormap from 0 to 3000? %% %% Challenge % Importing ASCII text files can be simple if those files contain only numbers % and if every row of data contains the same number of columns (i.e., a full matrix). % Whenever possible, you should strive to make your ASCII data files follow this organization. data = load('textdata_easy.txt'); % Life isn't always so easy, though. % You might have an ASCII data file that has different numbers of columns in different % rows or that has strings and numbers mixed together. % Before moving forward, use a text editor to inspect the file ?headache_data.txt.? % try: data = load('headache_data.txt'); % What happened? % Now read the code below and look for functions you don't know (fopen, % feof, regexp, etc). % This is an example of how can you write a code to read complex .txt % files. % Try to understand the code (use help and run each line separately an try % to understand the output). Comment each line of the code explaining what % the code is doing. % % Solution is provided below, read it after you tried and compare with your % comments. fid = fopen('headache_data.txt','r'); % behavioral_data=[]; % % % datarow=1; while ~feof(fid) % dataline = fgetl(fid); % dataline = regexp(dataline,'\t','split'); % % % % % if ~any(strcmpi('trial',dataline)) continue % end trial_column = find(strcmpi('trial', dataline)); choice_column = find(strcmpi('choice', dataline)); rt_column = find(strcmpi('rt', dataline)); accuracy_column = find(strcmpi('accuracy',dataline)); behavioral_data(datarow,1) = str2double(dataline{trial_column+1}); % behavioral_data(datarow,2) = str2double(dataline{choice_column+1}); % behavioral_data(datarow,3) = str2double(dataline{rt_column+1}); % behavioral_data(datarow,4) = str2double(dataline{accuracy_column+1}); % datarow=datarow+1; % end fclose(fid); % %% SOLUTION!!!! % TRY TO SOLVE BEFORE LOOKING THE SOLUTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Advanced importing text data % Here we borrow from C language to flexibly read in mixed data. Let's say % you have some poorly organized behavioral data files to read in, but at % least you know what text strings to look for: fid = fopen('headache_data.txt','r'); % fid is a pointer to a location on the physical hard disk (similar to how % we used variables as handles to axes when plotting). The 'r' means read % (later we'll use 'w' for write). % In this particular example, we will extract the trial number, subject % choice, reaction time (RT), and accuracy for each trial. Fields are separated by tabs. behavioral_data=[]; % initialize... we can't initialize the full matrix, because we don't know how big this will be. % The following code will remain inside a loop, reading in and processing new % lines of data, until we reach the end of the file. datarow=1; while ~feof(fid) % feof tests whether we're at the end of the file. dataline = fgetl(fid); % read a line ("file get line") dataline = regexp(dataline,'\t','split'); % regexp can be used to cut data according to delimiters. Here we will % cut this string of characters into a cell array in which elements of % the array are separated by tabs. % See next cell for more examples using regexp. % here we use strcmpi to compare strings. The "i" means to ignore case. if ~any(strcmpi('trial',dataline)) continue % continue means to skip to the next iteration of the loop. end trial_column = find(strcmpi('trial', dataline)); choice_column = find(strcmpi('choice', dataline)); rt_column = find(strcmpi('rt', dataline)); accuracy_column = find(strcmpi('accuracy',dataline)); behavioral_data(datarow,1) = str2double(dataline{trial_column+1}); % Note that we didn't initialize the size of the variable "behavioral_data" so matlab gives a warning. behavioral_data(datarow,2) = str2double(dataline{choice_column+1}); % If the variable is relatively small, it doesn't matter. behavioral_data(datarow,3) = str2double(dataline{rt_column+1}); % If the variable is large, however, it's best to initialize it to something really big, and then cut it down to size afterwards. behavioral_data(datarow,4) = str2double(dataline{accuracy_column+1}); % See chapter 4 in the book for further discussion of matrix initializations. datarow=datarow+1; % increment row end fclose(fid); % don't forget to close the file after you finish it!