30 2 Data Handling those vaees stored in that ar TIE accordintrer that logical sizes are way to remove elements from a vector.So we can create the vector d with the following simple command line: >d=a((a>=7)1(a<2)) d= 1 -2 726 Logical indexing is very useful whenne varable variable.Let'ssuppose we have some data in the ving experimen which is based on the Posner cueing paradigm(Posner 1980).Subjects are asked to react as fast as possible to the appearance of a target that can appear to either the left or the right of a central fixation point.Before the target appears,a cue indicates its location,but this cue has a limited validity (e.g.70%of the trials). The cue's validity (or invalidity)can be represented with Is (or Os)in the validity vector.Subjects'resp onse times,in seconds are stored in the vector .And these are the data we have collected so far: >R =0.90,0.55,1.01,0.33,0.442,0.51,0.85,0.441 >CueVa1 idity■1 ogica1([0,1,0,1,1,1,0,11): Response time can be categorized according to cue validity in the following way: >RTvalid RT(CueValidity) RTvalid 0.55000.33000.44200.51000.4400 >>RTinvalid-RT(-Cuevalidity) RTinvalid 0.90001.01000.8500 MATLAB has a number of logical functions operating on scalars,vectors,and matrices.Some examples are given in the following table.The examples use the following vectors: >clear all >a=[136317]: > >5 >d-1: I MATLAB function Description Example 1 Example 2 any (x) Return the logical l (true)if any elemen of the vector is a nonzero number.For 1 (continued)
30 2 Data Handling d contains those values stored in a that are TRUE according to c . Note, however, that logical vectors, such as c, can be used to index another vector only when their sizes are identical. Indexing through logical vectors is a very practical way to remove elements from a vector. So we can create the vector d with the following simple command line: >> d = a( (a>=7) | (a<2) ) d = 1 -2 7 26 Logical indexing is very useful when one variable is used to categorize a second variable. Let’s suppose we have collected some data in the following experiment, which is based on the Posner cueing paradigm (Posner 1980 ) . Subjects are asked to react as fast as possible to the appearance of a target that can appear to either the left or the right of a central fi xation point. Before the target appears, a cue indicates its location, but this cue has a limited validity (e.g. 70% of the trials). The cue’s validity (or invalidity) can be represented with 1s (or 0s) in the CueValidity vector. Subjects’ response times, in seconds are stored in the vector RT . And these are the data we have collected so far : >> RT =[0.90, 0.55, 1.01, 0.33, 0.442, 0.51, 0.85, 0.44]; >> CueValidity = logical([0 , 1, 0, 1, 1, 1, 0, 1]); Response time can be categorized according to cue validity in the following way: >> RTvalid = RT(CueValidity) RTvalid = 0.5500 0.3300 0.4420 0.5100 0.4400 >> RTinvalid = RT(~CueValidity) RTinvalid = 0.9000 1.0100 0.8500 MATLAB has a number of logical functions operating on scalars, vectors, and matrices. Some examples are given in the following table. The examples use the following vectors: >> clear all >> a=[1 3 6 3 1 7]; >> b=a>5; >> d=[ ]; MATLAB function Description Example 1 Example 2 any(x) Return the logical 1 (true) if any element of the vector is a nonzero number. For matrices, any(x) operates on the columns of x, returning a row vector of logical 1s and 0s >> any(a) ans = 1 >> any(b) ans = 1 (continued)
Types of Variables (Logical Values.Strings.NaN.Structures.Cells) 31 (continued) MATLAB function Description Example I Example 2 a11(x) Retum the logical I(true)if all the >a11(a) >>a11(b) elements of the vector are nonzero. ans ans or of logical Is and Os exist ('A') Check whether variables or functions are >>ex1st('c·)>> defined.0 if A does not exist.I if Ais a ans exist("a'》 variable in the workspace ans sempty(x) Retum the logical (true)for the empty >isempty(a) 7 empty(d) 0 ans= 1 Strings A string is a variable containing characters instead of numbers.Strings can be used to record subiects'names or any other type of textual informati n.A string is sig ned toa variable by enlosing it within apostrophes as in the following exampl >namestr -'Anne'; >whos name Name size Bytes class Attributes namestr 1x4 8 char The string'Anne'is composed of four characters,and the variable namestr is a 1x4 row vector of characters.The second letter of the name can be accessed in the following way: >name(2) ans n If you want to include a string containing an apostrophe,the apostrophe must be repeated: >sentence-'Anne''s dog is Buddy' Because strings are vectors,they may be linked with square brackets.e.g. >name ='Andrea'; >surname 'Palladio'; >fullname-[name,'',surname]
Types of Variables (Logical Values, Strings, NaN, Structures, Cells) 31 MATLAB function Description Example 1 Example 2 all(x) Return the logical 1 (true) if all the elements of the vector are nonzero. For matrices, all(x) operates on the columns of x, returning a row vector of logical 1s and 0s >> all(a) ans = 1 >> all(b) ans = 0 exist('A') Check whether variables or functions are defi ned. 0 if A does not exist, 1 if A is a variable in the workspace >> exist('c') ans = 0 >> exist('a') ans = 1 isempty(x) Return the logical 1 (true) for the empty array >> isempty(a) ans = 0 >> isempty(d) ans= 1 Strings A string is a variable containing characters instead of numbers. Strings can be used to record subjects’ names or any other type of textual information. A string is assigned to a variable by enclosing it within apostrophes as in the following example: >> nameStr = 'Anne'; >> whos name Name Size Bytes Class Attributes nameStr 1x4 8 char The string ‘Anne’ is composed of four characters, and the variable nameStr is a 1 × 4 row vector of characters. The second letter of the name can be accessed in the following way: >> name(2) ans = n If you want to include a string containing an apostrophe, the apostrophe must be repeated: >> sentence='Anne''s dog is Buddy' sentence = Anne's dog is Buddy Because strings are vectors, they may be linked with square brackets, e.g., >> name ='Andrea'; >> surname = 'Palladio'; >> fullname=[name,' ',surname] (continued)
32 2 Data Handling fullname Andrea palladio >whos name surname fullname Name Attributes fullname char name 1x6 char surname 1x8 16 char Note that we have put a space character between the name and the surname to separate them.The result is a vector of 15 characters:6 characters belong to the name.8 to the surname.and I for the space Now let's suppose you want to create an array of names.each row for one name Because nar in len gth.we need to use the char function.Indeed,if we folowingway we get >NameList=['John','Milly','Giovanni']; ??Error using ==vertcat CAT arguments dimensions are not consistent. Function char()overcomes the problem: >NameList2=char('John','Milly','Palladio') NameList2= John Palladio MATLAB has many functions for working with strings,which are listed in the following table.Use the string Mystring='vision Search',str1='hello' and str2='help'to get some practice with them: Function Description Example int2srtr(n) Convert numeric arguments >IntstrIng=int2str(25): into a string num2str(n) Convert numeric arguments into a string numString=num2str(23.4) lower (s) Convert a string into a lowercase >lower (Mystring) string vision search upper(S) upper(Mystring) VISTON SEARCH remp (S1,S2) Comnare the s rines sl and S2 and retum true (if strings ar remp(,t) identical.and false (0)otherwise 0 (continued)
32 2 Data Handling fullname = Andrea Palladio >> whos name surname fullname Name Size Bytes Class Attributes fullname 1x15 30 char name 1x6 12 char surname 1x8 16 char Note that we have put a space character between the name and the surname to separate them. The result is a vector of 15 characters: 6 characters belong to the name, 8 to the surname, and 1 for the space. Now let’s suppose you want to create an array of names, each row for one name. Because names can differ in length, we need to use the char function. Indeed, if we implement the variable NameList in the following way, we get an error. >> NameList=['John', 'Milly', 'Giovanni']; ??? Error using ==> vertcat CAT arguments dimensions are not consistent. Function char() overcomes the problem: >> NameList2=char('John','Milly','Palladio') NameList2 = John Milly Palladio MATLAB has many functions for working with strings, which are listed in the following table. Use the string MyString = 'Vision Search' , str1 = 'hello' and str2 = 'help' to get some practice with them: Function Description Example int2srtr(n) Convert numeric arguments into a string >> IntStrIng=int2str(25); num2str(n) Convert numeric arguments into a string >> numString=num2str(23.4); lower(S) Convert a string into a lowercase string >> lower(MyString) ans = vision search upper(S) Convert a string into an uppercase string >> upper(MyString) ans = VISION SEARCH strcmp(S1,S2) Compare the strings S1 and S2 and return true (1) if strings are identical, and false (0) otherwise >> strcmp(str1,str2) ans = 0 (continued)
Types of Variables(Logical Values,Strings.NaN.Structures.Cells) 33 (continued) Function Description Example strrep(S1,S2,S3) Replace all the occurrences of the >>strrep(str1,'1l0','avy') string S2 in the string SI with the string S3 heavy findstr(s1.82) Return the starting indices of any >findstr(strl,'1') ans 3 strmatch(S1,CAR)) >strmatch('he',str3) ans with the st 1 returns the matching row indices 2 disp(S1) Displays the array S1.without printing the array name 8p2:34 3 4 Tocreate formatted strings there isanother: vab)Let us see an example.Type the following con mm ands >RTmean=[0.431,0.321]: >Pos=char('left','right'); >sprintf('The RT for objects in &s position is 81.1f sec.',. Pos(1,:),RTmean(1)) ans The RT for objects in left position is 0.4 sec. osrintf('The RT for objects in ss position is 81.3f sec.' 2,:)RTmean (2)) ans The RT for objects in right position is 0.321 sec. Note that the three ellipsis points.allow you to continue the command in the next line. The function sprintf()contains the format argument and some variables.The format argument is a string containing ordinary characters and conversion specifica- tions.A conversion specification controls the notation.the alignment,the significant digits the field width and othe ts of the utp cifica 刀with the e c aracter There are rac ome of these are pres in the following table;howeverfor a complete list of them,refer to the MATLAB help
Types of Variables (Logical Values, Strings, NaN, Structures, Cells) 33 Function Description Example strrep(S1,S2,S3) Replace all the occurrences of the string S2 in the string S1 with the string S3 >> strrep(str1,'llo','avy') ans = heavy findstr(S1,S2) Return the starting indices of any occurrences of the shorter of the two strings in the longer >> findstr(str1,'l') ans = 3 4 strmatch(S1,CAR) Look through the character array CAR to fi nd strings beginning with the string contained in S1, returns the matching row indices >> strmatch('he',str3) ans = 1 2 disp(S1) Displays the array S1, without printing the array name >> disp(str3); hello help >> disp([1 2; 3 4]); 1 2 3 4 To create formatted strings there is another useful function: sprintf(format, variables) . Let us see an example. Type the following commands: >> RTmean=[0.431,0.321]; >> Pos=char('left', 'right'); >> sprintf('The RT for objects in %s position is %1.1f sec.',. Pos(1,:),RTmean(1)) ans = The RT for objects in left position is 0.4 sec. >> sprintf('The RT for objects in %s position is %1.3f sec.',. Pos(2,:),RTmean(2)) ans = The RT for objects in right position is 0.321 sec. Note that the three ellipsis points . allow you to continue the command in the next line. The function sprintf() contains the format argument and some variables. The format argument is a string containing ordinary characters and conversion specifi cations. A conversion specifi cation controls the notation, the alignment, the signifi cant digits, the fi eld width, and other aspects of the output format. Conversion specifi cations begin with the % character. There are also special characters beginning with the / character. Some of these are presented in the following table; however, for a complete list of them, refer to the MATLAB help. (continued)
34 2 Data Handling Conversion Descrintion Example Single character >sprintf('character:e,c character:c Decimal notation(signed) >>sprintf('integer:&d',12) There is an implicit ans 的 Integer:12 >c=5.12345 rintf('oat:43.1',c) inserted between the%and ans ols to specify the loat:5. he fra ('foat:3.) respectively 0omt:5.123 String of characters s='test' >sprintf('string:&s',s) New line sprintf('go to In new line') new line Horizontal tab >sprintf('Test the \t tab \t tab' ans Test the tab tab seful fun ction is inp ut().It waits for input from the keyboard,ending with the E TER key.Type the following: >input ('How old are you?' How old are you?35 ans 35 By defau()takes numbers as itsargument.If we eed strings instead. we need to add the optional argument's'to the input call: >input('How old are you?','s') How old are you?thirtyfive ans thirtyfive
34 2 Data Handling Conversion specifi cation and special characters Description Example %c Single character >> sprintf('character: %c','c') ans = character: c %d Decimal notation (signed). There is an implicit conversion from number to string without use the int2str function >>sprintf('integer: %d',12) ans = integer: 12 %f Fixed-point notation. A decimal number can be inserted between the % and f symbols to specify the size of the integer part and the fractional part, respectively >> c=5.12345; >>sprintf('float: %3.1f',c) ans = float: 5.1 >>sprintf('float: %3.3f',c) ans = float: 5.123 %s String of characters >> s='test'; >> sprintf('string: %s',s) ans = string: test \n New line >> sprintf('go to \n new line') ans = go to new line \t Horizontal tab >> sprintf('Test the \t tab \t tab') ans = Test the tab tab Another useful function is input() . It waits for input from the keyboard, ending with the ENTER key. Type the following: >> input('How old are you? ') How old are you? 35 ans = 35 By default, input() takes numbers as its argument. If we need strings instead, we need to add the optional argument ‘s’ to the input call: >> input('How old are you? ','s') How old are you? thirtyfive ans = thirtyfive