Getting Started with Matlab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to another video this is the first video in our series of tutorials designed to get you working with MATLAB today's video focuses on the simple task of just getting started with MATLAB so we'll look at starting the program setting up some preferences getting a layout set out and some very simple interaction with MATLAB we'll save the more complicated material for other dedicated tutorial videos so with that being said let's go ahead and get started all right so why don't we go ahead and just start MATLAB so if you've installed it on your machine one of the easiest ways to start it it's just obviously go to the Start menu and search for MATLAB or also during the installation process typically it's going to put a icon to the executable here on your desktop so you can go ahead and double click on that to get the program started which is what I'm gonna do right now and you might get a little splash screen with some startup information unfortunately that's on another screen of my computer right now so you're not actually seeing the splash screen come up but it's actually negotiating a license because I have a version of MATLAB installed on my machine which requires me to communicate with a licensed server but after that happens and I successfully I'm able to check out a licensed MATLAB will start up and let me just drag it over here this is what MATLAB might look like for you depending on how you your administrator has got your system set up the Start screen or the first screen that you see upon starting MATLAB might look a little bit differently but this is actually what we want to set up right now so to make this a little easier let me go ahead and maximize this so we can see the entire MATLAB window here and let's go ahead and do a few things like set some preferences and add a layout that makes a little bit easier to get some work done all right so typically matlab's default preferences will be set appropriately but let's just double check so if you ever want to access any of the MATLAB preferences just come up here to the home button click on preferences and there's a couple places that we may want to look here so one of the first things I'd like to point out here is if you just come here to fonts you'll notice here you'll be able to pick what type of font and more importantly especially for people like me that maybe don't have the best eyes Oh what size font do you want so typically you male have uses 10 which I've actually grown accustomed to but I know a lot of people think that that's uh that's pretty small and hard to look at so if you want to increase this this is a very easy place to increase the size of your font so when you're writing code it's a little bit easier to see again I happen to have grown accustomed to ten so let's just go ahead and leave that at ten the other place that I'd like to draw your attention to here is if you come here to editor slash debugger and then come here to backup files earlier versions of MATLAB used to have this option checked by default here and it was actually very annoying because what you would see is that every so often every well apparently every five minutes as it shows here in this setting it would save an extra file which would be a autosave file which would help you backup in case you lost something or in case something got corrupted but in this day and age I think with everyone hopefully locking down your files under some kind of version control system having a backup and having an extraneous file in your file structure is a little bit cluttering and it's a little bit cumbersome so I really like to make sure that this is always checked to be off before I get started so again there are plenty of other preferences there's tons of things you can mess around with in here but to start off I just wanted to draw your attention to those two again just make sure that the autosave is off that's all we wanted to do all right so since I didn't make any changes I'll just go ahead and cancel out of these preferences and we can now think about adding some extra windows to the environment all right so what I mean by windows here is that MATLAB is an integrated development environment right or an IDE it should have all of the tools that you need in order to write code execute code debug it explore the file structure all of that other kind of good stuff so you may want to add a couple of those tools here to the MATLAB environment so that you really don't need to leave MATLAB when you're working on some software so to do that what I would recommend doing here is again come here to the Home tab and come out here to layout and then down here in the show section there are all these extra windows that you can add to the viewpoint so what I would suggest your is let's let's turn on the current folder you see this pops up over here on the side I'll also come back here and maybe I'd also like to see the workspace and you may also want to see panel titles let's just turn on a couple more of these here maybe the tool strip something like that this looks pretty pretty reasonable and um just to illustrate this everything here ended up being docked here in my IDE you see everything is in one in the MATLAB window here so if I minimize this and move it around everything comes together but it might happen that one of these are broken off and come off apart as a as a completely separate window here right so that might happen to you if you want to fix that all you need to do here is come to this little icon here in the upper right corner here click on that and come here to doc what doc will do is it will slam that here into again the one MATLAB window so now yeah you've got one single window which has all of these different tools ready for you to work with now the last thing that I like to pull up here or add as a window here is the the the file editor or the text editor this is where you're going to end up writing a lot of your code so in order to get that to show up what I'm gonna do here is I'm just gonna come over and click on the new script button you can also come here and new and just say script okay so let's just click on this button here new script and you again we see this editor show up and at once again it's very likely that when you clicked on that button if you haven't set this up before that the code editor or the text editor will talk a little bit more about it in a second here but this editor might show up in a completely second window again if you want to fix that all you got to do is look for this little icon here in the top right and click on the doc editor here and again that will jam that into the single MATLAB window here so now we've got everything laid out here but it's it's maybe not the way that you would prefer what I like is I like to rearrange this so that usually I'm gonna be writing a lot of the code here in the editor so I like that to take up a lot of real estate this command window over here is where you're actually going to be entering in commands on the fly so if you want to do some rapid testing or iterations you can you can enter commands into the command window some of these other things like the current folder will show you the file structure of where you're actually working right now so you can see which other scripts or files or other objects are in the same folder and we're gonna see later on down here this workspace is going to be showing you all of the different variables that are currently in scope we'll get into all of these a little bit later but again for this getting started video I just want to show you a feature here now that you have all of the different windows set up you can actually move them around and manipulate the layout so that it suits your workflow or your liking so what I like to do is let's go ahead and maybe drag this current folder maybe I will oh this is nice maybe I like it to be tabbed behind the workspace so as you can see I can flip between both of these and they don't take up too much space um and this is actually very close to my preferred workspace layout so if I kind of resize these and get them to be what I'd like again I have a big space for writing code I have a reasonably sized space for entering in commands in the command window and then I'm gonna share this little corner down here between the workspace and the current folder that I can flip between with the mouse click if you're happy with this layout one of the things you can do here is if you actually come over to home and you click on layout and you can go ahead and hit save layout because that's something that you would like so I'm gonna go ahead and call this how about my demo layout and hit OK great so now this is saved so if ever for some reason your layout gets messed up in the future I don't know maybe it looks like this I'll just pick this somebody came to your workstation and messed around with the settings and now you've got this weird layout that you're not familiar with you can again just come up here to the home layout and select the layout that you saved and bam you're back to where you were and you're ready to do some work so great that shows you how to get your windows in your layout setup why don't we explore using a few of these different windows here to actually get some work done all right so the first thing I'd like to draw your attention to is actually the command window up here that we've jammed in the upper right corner you can actually use MATLAB like a very simple calculator via the command window so for example if you want to know what is 3 plus 7 here just type it in there and hit enter and it'll give you the answer notice here that as soon as you entered that here down in the workspace you got a variable tufa declared and created that's called ans or answer with a value of 10 here this is a very interesting if you think about this a little longer and for those some of you that might have some experience with other languages like C C++ or C sharp here you'll notice that MATLAB actually dynamically allocates memory to store its variables unlike some of those languages we mentioned earlier so you don't actually have to declare the variables before using them so in this case it just made this variable called ans to store the answer of my query of 3 plus 7 so we can extend this concept here so for example if I just want to make a variable called X and I can assign that thing to the value of 3 and I'll just hit enter oh let's do another one how about Y it gets two and then what I also like to point out here is let's go ahead and put a semicolon to the end of this and we'll look at the difference so as you can see when you put the semicolon there it actually suppresses the output to the command window so the result of that will not be displayed out here in the command window but both of these variables x and y are still going to be created down here in the workspace regardless if they if the output was suppressed or not so now you can start doing very simplistic operations like Z is X plus y right then I enter that it will just go ahead and do as you expect right all right what else might we want to do here well you notice here that the command window is getting a little bit cluttered at this point you might want to clear this command window so let's go ahead and use our first matlab function which is CLC so if you just type in CLC and hit enter you'll see that what that did is it cleared the entire command window so now might be a really good time to also mention the command help so if you type in help space the name of any matlab function then you can actually learn a little bit of information about that function so in this case remember we use this command CLC so if I type help space CLC and hit enter we're gonna get a little bit of documentation here telling you about what that command does and how to use it it also gives you some suggestions on other functions that you might want to do and it's gonna give you a nice hyperlink to a page where if you click on this MATLAB will actually bring up a nice set of documentation on that function so it's a really good interface to get some help on different functions how to call them how are they used why would they be helpful all that sort of stuff so let me go ahead and close the command the help documentation here and again I'll type in CLC to clear this command window you'll notice here that when I cleared the command window all that did was basically wipe the slate clean up here it didn't erase any of the variables that were down here in my workspace if I want to get rid of these variables down here and really start from scratch I might want to use clear so for example let's say help clear and you'll see if I scroll back up to the top here what this thing does is it basically clears variables and functions from memory which is exactly what I want to do if I want to for example get rid of all this stuff that's in the workspace so again let's scroll back down to the command window where I can put some input in and I'll just type in clear enter and you see the workspace variables go away and again if I type in CLC and hit enter I'm literally back to a blank slate here I have nothing right here in the command window and I have nothing down in the workspace now however let's say I want to go back and re enter in some of those commands that I already did like that X is equal to I can't remember what value do we use I think we said X is equal to 3 and Y is equal to 2 what if I wanted to change those so I'm gonna come here to the command window and if you just pushed the up arrow on your keyboard you're gonna see that it's gonna bring up this little dialog which shows you basically a diary of all of the different commands that you've done over the past several iterations of your session so here's a start of my session you'll see I started doing some stuff so I could say oh you know what X I don't really want that to be 3 I'll just click on this and I could say X is it was really supposed to be 5 and again I'll push the up arrow to go ahead and find some of these other commands Y really wasn't supposed to be sorry I double clicked on that which actually entered it and let me come back here and click on this come down here umm why wasn't supposed to be - it was supposed to be you know PI 3.14159 you know it's 3.1415 and actually MATLAB is pretty smart here and it's got a couple of these constants built in so if I just type in P I it actually knows what pi is so if I hit enter there check it out y is actually the value of pi great so I think we are now a little bit comfortable navigating the command window in the workspace but hopefully this last couple examples here where we were manually changing these values something should have been going clang in your head here and you should have said you know that is a pretty cumbersome way to actually make changes to your to your code into your calculations here right it will be much nicer if I actually had some script or a text file or some actual thing that looked like code where I could rapidly iterate and change how the software operates right rather than hunting and pecking around in my directory of commands here I really this is this is not a very conducive way to get a lot of high quality software and analysis done so let's go ahead and clear whoops I'm gonna have to get rid of all this space clear the command window and let's start from scratch so to do that I'd like to bring your attention now over here to the editor so the editor window is basically a text editor that's gonna allow you to write execute and debug MATLAB code within the IDE so one of the things we want to do is obviously let's start writing something down so I could first say you know X is equal to 3 y is equal to 2 and let's put the semicolon maybe on the Y command just so we can see the difference here and then we could say Z is equal to X plus y right and this is our code here it's really simple here but this is actually a little bit easier because we see all of our commands laid out in a row here where we're free to go on the back and change any of these at any point here so this is a much better workflow for actually writing complicated algorithms so let's go ahead and now try to have MATLAB execute this script or this code here so malloc typically refers to these as scripts so in order to execute that there's a couple of ways you can do this here one of the ways you can do this here is you come up here to the editor tab and you just click on this Run button and what it's gonna do is it's gonna execute the currently selected script here so if I do this MATLAB is actually gonna make me save this first because you'll notice I actually haven't saved this anywhere on my machine so let's go ahead and try this right now so I'm gonna hit f5 I'm gonna hit the Run button here and it's gonna bring me up with a save dialogue let's just go ahead and save this here and maybe my C Drive I'll go ahead and make a new folder here and call this getting started with MATLAB okay and I'll go ahead and I'll title this something like simple or how about my simple script and notice it saves it as a file extension dot animal ok so if I go ahead and hit save I'm gonna get one more dialog box the first time I try to execute this script so if we read this a little bit more closely what it says here is let me pull up that file here I'm gonna start a Windows Explorer instance here and I'll come here to my C Drive and if you remember we put this here and where was it something like here I can't even remember what do we call this thing it was something oh yeah getting started yeah I accidentally clicked on it yeah all right getting started with MATLAB here's the script that is trying to execute and if you notice here the path of where the script is located is located in C getting started with MATLAB here if I look over here in the MATLAB IDE MATLAB has a concept of a current working directory here which is actually listed right here so it says MATLAB thinks I'm working in C dev u-dub MATLAB u-dub MATLAB which is a different location than where the script is actually located so it's giving me this little warning here saying that hey that file that you saved it's not in the current folder here and I haven't told MATLAB about this path here to see getting started with MATLAB so it wants to know what do I want to do 99% of the time the right answer here is click on this change folder button all that's going to do is it's going to change the MATLAB current working directory to the location of wherever you save this script so I really I'm very rarely can think of an instance where you would ever want to hit this add to that path button here so what I would recommend doing is always just hit the change folder button here so as soon as I do this watch up here and you should see our current working directory gets switched to this location of C getting started with malloc so I'm gonna go ahead and hit that here it is this got changed to the current to that directory we talked about and if you look over here in the command window my script actually ran top down so it executed line 1 line 2 while line 3 was really nothing here and then it executed line for all in order here and you can see this over here because look at this X get equals 3 corresponds to line 1 because line 1 right we did not actually suppress the output here with the semicolon so it just got printed here to the command window lying to we did suppress the output with a semicolon so that's why you don't see any stuff about I'm being declared err or assigned a value here in the command window but it's still created here in the workspace because MATLAB actually executed that line of code here finally if you come down here the last line Z is equal to X plus y here you see it show up here in the command window and again you see it show up down here in the workspace so this is great here um let me show you a couple other things to do with the editor as you start writing more and more complicated code I'm sure you guys are gonna want to add comments to your code so you know what you're doing so let's just go ahead and add a couple of comments at the beginning of this year so I'll just hit enter a few times to enter comments into MATLAB the easiest way to do this is just type the % so percent is the start of a comment line here so um maybe I'll write my name I'll write my contact information and I'll write a little bit about what this script is doing here so this script is used to illustrate simple concepts with MATLAB okay and I'll go ahead and hit save here and what I can now do is I can just re-edit this or you know why don't I make a couple of changes while I'm at it I'll change this here at the pi/2 pi here and I can now go ahead and click on the Run button here to re execute the script or if you hover over this you'll notice that MATLAB is giving you another little hint here that you can actually press f5 to execute a script so if it's too much work for you to come up here and click on this Run button just go ahead and and save your script and then just hit f5 which I did right there and it goes a Henry execute the script here now you'll notice here that it's getting a little bit hard to understand where I executed each script I mean sure it gives me a little bit of a notation here I executed it for the first time here's the second time what I always like to do with all of my scripts here is the very first command I start with is a clear what that's going to do we saw earlier as soon as it hits line 5 it's going to blow away all of the variables that are defined in the workspace so I'm starting from scratch so I won't be surprised and have a variable with the value that I'm not sure on what the actual value is because it was left over the previous session I want to avoid that so the first thing I'm gonna do is I'm actually gonna clear everything and then the next thing I want to do here is let's clear the command window so this looks all nice and clean so I'll go ahead and hit CLC the last one and maybe I'll leave this as an exercise for you here for the reader for the viewer here is I always also put in a close all statement I'll let you read the manual on what this does um but really if you have any figures or plots open this will also close all of them so these three lines I put at the start of every single one of my scripts because that ensures I start from a clean slate here okay so now let's go ahead and add a comment here and say start simple calculations something like that so again if I just hit f5 it will actually automatically save this so I you know I was I know I was mentioning I I'm in the habit of hitting save a lot here but if you want to just hit f5 or hit the Run button it will actually automatically save the script while you're at it so tell you what let's just go ahead and hit f5 again and it ran it again here and everything got back to back to a clean slate before it was executed here alright so now that we've got this script set up I'll admit it's not the most complicated thing but I think we can use it to illustrate some examples about debugging so I'm sure any of you that have written the software before are probably wondering how do I pause execution of the script at certain locations so I can inspect how the program or the script is is executing here so if you notice all of these little horizontal dashes next to some of these lines what that's basically saying is these are locations where you can enable breakpoints so if I would like to enable a breakpoint all you have to do is just hit or click on that horizontal dash it will turn into a red dot here so let's put maybe uh let's put a few of them and scatter them around in different locations so now when I actually run my script or hit f5 the script will start executing from the top down here and it will pause when I reach one of these breakpoints here so you'll notice this green arrow showed up so all that's executed is the clear command and you'll notice it's way here on the CLC command you can see in the command window that it clearly has not executed yet so it's now waiting for user input so this is a great time if you want to inspect what the value of some different variables are or what variables are in scope at this point this is a great tool to do this so if you would like to continue again you can come up here to the editor and here's a couple of options you can continue running to the next breakpoint by just hitting f5 so if I do that control should jump all the way down to line ten why don't we just try that so there we go it executed the CLC it executed the close all now it's waiting on the x equals three command so let's go ahead and here's here's another option how about step so its instead of having hitting f5 I hit f10 this will just execute one line at a time so I'll go click aha that worked because yeah look it X is three that's kind of what I expected here so let's go and step one more time yep why looks around about right and let's just go ahead and step one more time and now the little down arrow tells me it's ready to step out or basically continue and finish so if I go ahead and continue there we go I am basically exited debugging at this point so at this point I think we've gotten um quite a bit done here and hopefully people are feeling a little bit more comfortable about how to work with MATLAB the last thing we might want to look at is now that we did all of this coding here it's it's simple but again it'll astray --tz-- the purposes what i'd like to do now is maybe i want to save this file here so maybe let's talk about what do you need to save in matlab to make sure that you're able to come back or send us to a friend or a colleague and basically being able to pick up where you left off the next time you want to continue your work so to do that let's bring up my windows explorer again here and again go to this location here see getting started with matlab and my simple script on m and it's sitting right here and if you notice matlab is great because matlab code is literally just ascii text files and there's just one thing to tote around right now it's just the thing that has the dot ma tension here so here's my my algorithm my script so I don't need any other secondary files I can just email or save this single file to my version control or to my hard drive or whatever I want um to prove that to you if you just right-click on this and you just edit this thing with some text editor you know like open this thing with notepad here I'll do that let me drag drag it over you'll see it's basically I mean it is the exact same thing they're just ASCII text files here of course it's much nicer to edit this in MATLAB here because MATLAB gives you the nice color coding and all of that other kind of good stuff but if you're in a pinch and you want to write some MATLAB code it's really just write it in a text editor here and you're good to go so again if you go ahead and just save this file to wherever you want and you'll be ready to pick up where you left off and continue with your MATLAB experience here so with that I think we've covered most of what I wanted to talk about in this very simple getting started with MATLAB tutorial so let's quickly take a look at the summary of what we've covered today all right so to summarize what we talked about today we first looked at how to go ahead and start MATLAB we then looked at modifying preferences here we've then covered how to activate various different windows in the MATLAB IDE and also set that up as a custom layout we then looked at how to use the command window to enter in simple commands or and make simple calculations here and then we quickly graduated into using the editor to write actually more complex scripts here that could be edited and debugged a lot easier which naturally led into our discussion of writing commenting and how to debug code within the MATLAB IDE and then finally the last thing we looked at here was once you are all done how should I save or more importantly what source code files do I need to save to make sure I don't lose any work when I want to come back to this so hopefully this is a good way to get you started using MATLAB here and I hope you'll join us for some future videos where we will dive into some more advanced features and functions of MATLAB so until the next video I hope you have a good one I'll talk to you later bye
Info
Channel: Christopher Lum
Views: 24,348
Rating: 4.984221 out of 5
Keywords: Matlab, Matlab Tutorial, Getting Started with Matlab, Matlab Lesson
Id: _M0mOHn0ink
Channel Id: undefined
Length: 29min 8sec (1748 seconds)
Published: Wed Sep 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.