1 00:00:06,960 --> 00:00:09,600 PROFESSOR: This is Dr. MATLAB, lecture 3. 2 00:00:09,600 --> 00:00:12,690 If you're using MATLAB on the command line for enough time, 3 00:00:12,690 --> 00:00:15,720 you realize that you really want to save a bunch of commands 4 00:00:15,720 --> 00:00:20,370 in a file and then execute all those commands in one go. 5 00:00:20,370 --> 00:00:22,770 This helps you so that if you have typos, 6 00:00:22,770 --> 00:00:26,010 you can correct the typos without introducing new ones. 7 00:00:26,010 --> 00:00:29,160 It also allows you to create large pieces of code 8 00:00:29,160 --> 00:00:35,340 that call each other and really have complicated projects. 9 00:00:35,340 --> 00:00:37,680 To see how this works, we need an example. 10 00:00:37,680 --> 00:00:41,560 What we'll do is generate the Fibonacci sequence. 11 00:00:41,560 --> 00:00:44,730 So in the Fibonacci sequence, the first two terms are 1. 12 00:00:50,050 --> 00:01:03,030 And then each term is the sum of the previous two, like that. 13 00:01:03,030 --> 00:01:05,239 Notice that this will create a new term 14 00:01:05,239 --> 00:01:08,704 in f that will be the sum of the very last one 15 00:01:08,704 --> 00:01:09,870 plus the second-to-last one. 16 00:01:12,740 --> 00:01:16,520 Here it created 2, the sum of 1 plus 1. 17 00:01:16,520 --> 00:01:18,380 If we run that command again, it will 18 00:01:18,380 --> 00:01:25,190 generate 3, and then 5, and then 8, and then 13, et cetera. 19 00:01:25,190 --> 00:01:28,220 So now I want to take these three important lines-- 20 00:01:28,220 --> 00:01:32,420 this one, this one, and this one-- 21 00:01:32,420 --> 00:01:35,030 and create a file that will do all of this 22 00:01:35,030 --> 00:01:37,160 and put this line inside a loop so that it 23 00:01:37,160 --> 00:01:40,740 does that, say, 20 times. 24 00:01:40,740 --> 00:01:43,730 To do that, I open the History browser. 25 00:01:50,300 --> 00:01:53,498 I want it to be docked. 26 00:01:53,498 --> 00:01:56,180 And it's docked. 27 00:01:56,180 --> 00:02:00,502 And here I find my commands that I issued here. 28 00:02:00,502 --> 00:02:03,560 I'm going to select all of them, the three commands, 29 00:02:03,560 --> 00:02:07,160 right click, and copy. 30 00:02:07,160 --> 00:02:09,710 So now I've copied these three commands. 31 00:02:09,710 --> 00:02:11,645 Over here, I can open a new script. 32 00:02:14,546 --> 00:02:16,940 The first thing I want to do is get rid of this bar 33 00:02:16,940 --> 00:02:21,240 because it uses up a lot of important space. 34 00:02:21,240 --> 00:02:23,930 So now I have a bit more space, and I 35 00:02:23,930 --> 00:02:26,210 can close this command history because I 36 00:02:26,210 --> 00:02:27,770 have copied those commands. 37 00:02:27,770 --> 00:02:31,160 And now I'm going to paste. 38 00:02:31,160 --> 00:02:32,660 After I've pasted this, of course, 39 00:02:32,660 --> 00:02:33,950 I need to clean up a bit. 40 00:02:33,950 --> 00:02:37,370 So I'm going to add semi-colons at the end of these commands 41 00:02:37,370 --> 00:02:40,070 because I don't need to see the output. 42 00:02:40,070 --> 00:02:43,500 And I'm going to add a for loop that 43 00:02:43,500 --> 00:02:47,750 will calculate the first 20 to the next 20 terms. 44 00:02:47,750 --> 00:02:49,520 So I'm going to have 22 in total-- 45 00:02:49,520 --> 00:02:52,190 two from here, and then another 20 from here. 46 00:02:52,190 --> 00:02:55,010 At the end, add a Tab character just 47 00:02:55,010 --> 00:02:57,900 for alignment to make it easy to read. 48 00:02:57,900 --> 00:03:02,870 And now at the end, I will just display f like that. 49 00:03:02,870 --> 00:03:06,020 Because everything else is suppressed. 50 00:03:06,020 --> 00:03:09,350 I need to save this. 51 00:03:09,350 --> 00:03:11,090 I need to select a place to save it. 52 00:03:11,090 --> 00:03:14,570 The default is in Documents, Matlab. 53 00:03:14,570 --> 00:03:16,220 I want to save it in a different place 54 00:03:16,220 --> 00:03:20,560 to demonstrate what the path is. 55 00:03:20,560 --> 00:03:24,190 So to do this, I open a new folder. 56 00:03:24,190 --> 00:03:29,358 I will call it Scripts, for example. 57 00:03:29,358 --> 00:03:32,110 So now this script is under Matlab. 58 00:03:32,110 --> 00:03:35,770 And then in Scripts, I will call this file Fibbo. 59 00:03:35,770 --> 00:03:38,020 It already is an M file, which is the extension 60 00:03:38,020 --> 00:03:39,360 that you need to have. 61 00:03:39,360 --> 00:03:41,290 Fibbo stands for Fibonacci. 62 00:03:41,290 --> 00:03:42,630 And I save it. 63 00:03:42,630 --> 00:03:46,360 And now I go to my command line and I try to run it, 64 00:03:46,360 --> 00:03:47,590 and it doesn't run. 65 00:03:47,590 --> 00:03:49,600 And the reason it doesn't run is that Fibbo 66 00:03:49,600 --> 00:03:53,230 is sitting inside a subdirectory of Matlab. 67 00:03:53,230 --> 00:03:59,575 And my current directory, found by typing CD, is Matlab. 68 00:03:59,575 --> 00:04:01,580 So I'm sitting in Matlab. 69 00:04:01,580 --> 00:04:06,230 Fibbo is sitting in Scripts, which is inside Matlab. 70 00:04:06,230 --> 00:04:12,560 We can see this by looking at the current folder. 71 00:04:12,560 --> 00:04:15,310 So here's the current folder. 72 00:04:15,310 --> 00:04:19,120 Under Scripts, there's Fibbo, but we are not there. 73 00:04:19,120 --> 00:04:20,890 We're up here in Matlab. 74 00:04:20,890 --> 00:04:22,510 So there's two ways we can solve this. 75 00:04:22,510 --> 00:04:26,450 We can either change directory to Scripts-- 76 00:04:26,450 --> 00:04:28,130 for example, by double clicking here. 77 00:04:28,130 --> 00:04:30,600 Now our current folder is Scripts. 78 00:04:30,600 --> 00:04:33,860 Now if we go here and call if Fibbo, 79 00:04:33,860 --> 00:04:37,112 it works, and it calculates the 22 terms 80 00:04:37,112 --> 00:04:38,195 of the Fibonacci sequence. 81 00:04:41,095 --> 00:04:43,220 We could have also done this from the command line. 82 00:04:43,220 --> 00:04:45,650 Here I'm going to go back to MATLAB. 83 00:04:45,650 --> 00:04:50,840 And I can say change directory scripts, Tab completion. 84 00:04:50,840 --> 00:04:53,050 And now I'm in Scripts, and I can call Fibbo again. 85 00:04:57,050 --> 00:04:58,340 Another thing I could do-- 86 00:04:58,340 --> 00:04:59,870 going back to MATLAB-- 87 00:04:59,870 --> 00:05:02,070 is to add scripts to the path. 88 00:05:02,070 --> 00:05:02,800 What is the path? 89 00:05:02,800 --> 00:05:04,180 Let's see. 90 00:05:04,180 --> 00:05:08,897 So if I type path, I get a list of directories, 91 00:05:08,897 --> 00:05:10,480 a long list of directories that Matlab 92 00:05:10,480 --> 00:05:14,590 uses in order to look for the commands that I issue. 93 00:05:14,590 --> 00:05:16,090 All the commands that you issue will 94 00:05:16,090 --> 00:05:19,240 have to be in one of these directories for MATLAB 95 00:05:19,240 --> 00:05:23,560 to look at them, or in the current directory. 96 00:05:23,560 --> 00:05:27,030 So you see that the Matlab directory, the Documents Matlab 97 00:05:27,030 --> 00:05:30,010 directory, is always in the path. 98 00:05:30,010 --> 00:05:32,480 That's a default. But you can add 99 00:05:32,480 --> 00:05:34,100 to the path whatever you want. 100 00:05:34,100 --> 00:05:44,501 So for example, I can add add path scripts. 101 00:05:44,501 --> 00:05:46,750 Now that I've done that, let's look at the path again. 102 00:05:49,540 --> 00:05:51,815 Scripts is in there. 103 00:05:51,815 --> 00:06:00,130 And if I try to run Fibbo, then it works even though I'm here. 104 00:06:00,130 --> 00:06:04,860 The way I've written it, it will look in Scripts 105 00:06:04,860 --> 00:06:10,850 as a relative directory and not as an absolute directory. 106 00:06:10,850 --> 00:06:12,730 If I want an absolute directory, I 107 00:06:12,730 --> 00:06:27,760 would have had to say add path slash Users slash Documents 108 00:06:27,760 --> 00:06:30,352 slash Matlab slash Scripts. 109 00:06:32,950 --> 00:06:35,940 This is a different path because this is the exact one. 110 00:06:35,940 --> 00:06:37,780 Let's look at the path now. 111 00:06:37,780 --> 00:06:42,260 Here we have the absolute and the relative scripts. 112 00:06:42,260 --> 00:06:45,755 At any case, if you want to see whether Matlab can see a file, 113 00:06:45,755 --> 00:06:54,490 you can type which Fibbo, and then MATLAB tells you, 114 00:06:54,490 --> 00:06:57,820 if it sees it, which file it actually is going to. 115 00:06:57,820 --> 00:07:01,450 So if you have the same file name in different directories, 116 00:07:01,450 --> 00:07:03,940 both of which are in your path, the first one 117 00:07:03,940 --> 00:07:06,400 that Matlab encounters will be the one that MATLAB calls. 118 00:07:12,970 --> 00:07:16,620 Let's do one more thing. 119 00:07:16,620 --> 00:07:19,100 So let's see what happens when we actually hide something. 120 00:07:19,100 --> 00:07:21,530 This can be potentially dangerous. 121 00:07:21,530 --> 00:07:24,890 Let's open a new file. 122 00:07:24,890 --> 00:07:28,430 And we'll say-- it doesn't do much. 123 00:07:28,430 --> 00:07:30,500 Let's say it says x equals 1. 124 00:07:30,500 --> 00:07:31,940 And we're going to save this file, 125 00:07:31,940 --> 00:07:34,070 but we're going to be a bit insidious about it, 126 00:07:34,070 --> 00:07:35,361 and we're going to call it Sin. 127 00:07:39,140 --> 00:07:41,580 And we're saving it in the Matlab directory. 128 00:07:41,580 --> 00:07:46,200 So we're saving a file called Sin in the Matlab directory. 129 00:07:46,200 --> 00:07:51,230 And now if I do sin of 3, I get a very strange error. 130 00:07:56,680 --> 00:08:01,062 Notice that it's a function. 131 00:08:01,062 --> 00:08:02,520 But if I would have called it sine, 132 00:08:02,520 --> 00:08:04,145 I would have gotten the 1, because that 133 00:08:04,145 --> 00:08:05,820 would have been the script. 134 00:08:05,820 --> 00:08:09,870 So you have to be careful with things like this. 135 00:08:09,870 --> 00:08:13,560 And I would recommend not having such a file 136 00:08:13,560 --> 00:08:15,040 in your current folder. 137 00:08:23,420 --> 00:08:32,321 You can also execute a file from the graphical user interface 138 00:08:32,321 --> 00:08:33,320 by clicking this button. 139 00:08:35,970 --> 00:08:42,500 If that file is not in the path and not in the current folder, 140 00:08:42,500 --> 00:08:47,000 Matlab will ask you whether you want to change folders or add 141 00:08:47,000 --> 00:08:48,060 it to the path. 142 00:08:48,060 --> 00:08:51,750 And you can answer whatever makes sense to you. 143 00:08:51,750 --> 00:08:56,040 For a function, you need to add the keyword function 144 00:08:56,040 --> 00:08:58,560 in the beginning of your file. 145 00:08:58,560 --> 00:09:03,415 Function f to c. 146 00:09:06,500 --> 00:09:09,440 So let's write a function that accepts Fahrenheit and converts 147 00:09:09,440 --> 00:09:11,960 it to Celsius. 148 00:09:11,960 --> 00:09:15,920 So return value, name of the function, input. 149 00:09:19,830 --> 00:09:21,770 And now we want to say something like b 150 00:09:21,770 --> 00:09:30,305 equals n minus 32 times 5/9. 151 00:09:33,160 --> 00:09:36,620 Now v becomes this number. 152 00:09:36,620 --> 00:09:44,020 And that is the conversion between Fahrenheit and Celsius. 153 00:09:44,020 --> 00:09:48,880 So this value v will be returned to whoever calls this function. 154 00:09:48,880 --> 00:09:50,100 We need to save it. 155 00:09:50,100 --> 00:09:53,910 MATLAB already suggests f to c. 156 00:09:53,910 --> 00:09:58,470 And we can put it inside Scripts next to Fibbo. 157 00:09:58,470 --> 00:10:01,914 Now we can write it as a function f to c. 158 00:10:01,914 --> 00:10:03,300 We need to give it an input. 159 00:10:03,300 --> 00:10:06,610 If we don't give it an input, we get an error. 160 00:10:06,610 --> 00:10:10,290 So if we type 32, we get 0. 161 00:10:10,290 --> 00:10:17,060 If we type 101, we get this as a function. 162 00:10:21,170 --> 00:10:26,570 You can either end your function in the file or not. 163 00:10:26,570 --> 00:10:31,820 This mostly affects the behavior when you have sub-functions. 164 00:10:31,820 --> 00:10:34,309 And will talk about this in the future.