Welcome to Matlab Basics 2021 (Archive - Day 1 and 2)
This is an online course.
Day 1
Introduction: testing HackMD
Click on the "pencil" icon on the top right corner
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
. Then you should see the "code" of the page on the left, and the output on the right. Write on the left side. When you are done click on the "eye" icon
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
which now should be on the top left of the screen.
- Write your question like this
- an answer by someone who knows
- A comment with a
%% disp("bit of code pasted")
using backQuote "`"
- here a section with longer code using 3 backQuotes:
- another question here
-
Can we get recording of those lectures as well?
-
Can I get a certificate / credit for the course?
-
Can I get a certificate without the credit?
- As the number of learners enrolled is very high this year, we cannot provide a certificate for those who do not finish the homeworks (see point above)
-
Is the course free of charge?
- Yes, it is! However this course is made for learners at Aalto. Any differences specific to other organisations cannot be taken into account as we do not have the means for doing that (e.g. verifying that every organisation has access to the same version of Matlab)
-
Do I need an Aalto (student) account?
- No, you only need a working copy of Matlab and an acount on Matlab Academy website if you plan to do the homeworks for the certificate/credit. If your organisation does not have access to Matlab license or to Matlab academy, then unfortunately we are not able to compensate for that.
-
Will there be any Simulink?
IceBreaker 1
Where are you from? What do you study/work with?
- Helsinki, Kumpula. ML, Graphics and such.
- Aalto, Quantum Technology BSc student
- Helsinki (Käpylä). I work with brain images.
- Aalto, RSE, teaching support
- Helsinki, I study chemistry.
- Helsinki (Kumpula). Working on computational fluid dynamics with medical applications.
- Aalto, study CS, work as Data Scientist
- Oulu, Electrical engineering
- Aalto, computer science +
- Oulu, Computer science, Intelligent Orchestration of Microservices
- Oulu, Electrical Engineering
- Oulu, Electrical Engineering, Microelectronics & electronic materials
- Helsinki(kumpula) ,Computer Science
- Helsinki (Oulunkylä) Cornea imaging, PhD candidate
- Helsinki. I have a PhD in cognitive neuroscience/psychology and work with EEG/TMS at the department of digital humanities (cognitive science)
- Helsinki, Kumpula, Planetary science
- Oulu, Wirless Communications. Working with Electronic devices.
- Oulu University, Math team research; postdoc
- Helsinki, Kumpula, Geology, post-doc
- Aalto, Study Physics
- Aalto, physics
- Aalto, Applied physics, PhD candidate
- Helsinki, Kumpula, Planetary science, masters level student
- Helsinki, Chemistry
- Oulu, Space Physics and Astronomy.
- Aalto, chemistry
- Oulu, Chemistry.
- Oulu, Energy Engineering
- Jyväskylä/Helsinki, psychology/neuroscience, PhD candidate.
- CWC, oulu, university of oulu
- Oulu, Univ
- Oule, Univ
- Helsinki, Cell Biology
- Oulu, Environmental Engineering
- Oulu, Radiotechnology
- EJUST, Space Environment
- Oulu physical geography research unit
- Helsinki,Geology, Master student
- Helsinki, Master of Chemistry
- Moscow, MIPT, Ph.D. Planetary physics
- Aalto University, Food/Material Science
- Oulu, Space Physics
- University of Oulu, Oulu mining school, Lecturer Mineral processing
- University of Oulu, SGO, Ph.D.
Questions
-
Will there be further Matlab courses after this course? With new topics etc
- There are some courses by individual departments for all I know, but ScienceIT does not currently plan to run an advanced course. Maybe if there is enough interest there will in the future.
-
How is this course different from Mathworks' MATLAB or Simulink OnRamp?
- More direct feedback to questions.
-
Will we have a break or do 3 hours straight?
-
Break for how long?
Icebraker 2
What are your learning goals?
- Learn Matlab use as Samuli likes to use it.
- Just learn the basics
- Learn and refresh my memory on Matlab basics and then utilize it in data processing.
- Enhance my Matlab performance, get more matlab skills towards profficiency.
- to use Matlab for handing big datasets and do some simple calculations. -> overcome the restrictions of Excel
- Enhance my matlab skills
- Learn the basics of matlab.
- I want to learn the basics so I can continue on my own, and especially understand other people's codes better.
- Just to learn the basics and familiarity, I haven't used it before
- I want to learn basics
- Chemometrics +++++++++++
- I just want to learn the basics
- My research group has a lot of scripts written in Matlab, so I want to learn how to use and apply them
- Learn the basics
- More data analysis skills and use it in my paper
- Learn the Matlab basics and to apply it in my research work
- Learn the basics so I can understand and edit other's code
- I want to learn the basics since I haven't done a lot of programming before
- Recap the basics, would be interested to have advanced courses afterwards. I have special long term project, which is out of this course; How to vectorise old loop-based code and thus make it much faster. Then how to run it with GPU in Triton.
- Basics of Matlab
- Follow and understand work of colleagues/other researchers and participate in said work.
- Learn fundamentals and improve data analysis skills
- Refreshes my knowledge of matlab but of course don't have strong reason for usingmatlab
- I want to start learning matlab for data analyses
- Wanna learn the basics
- I am bit rusty with Matlab - so to regain basic skills
Matlab Academy and Software Carpentry Course
Questions here below
https://swcarpentry.github.io/matlab-novice-inflammation/
-
Break for how long?
- The break will be for ~10 minutes
-
Is the recording going to be available?
- Yes we will make it available and provide a link by e-mail
-
I have version 2017. Is it ok?
- You might run into some issues, but most things should work.
-
Could you please try to be close to the Mic?
- Maybe increase your output volume, I can hear him fine.
-
I have downloaded and zipped the file but not uploaded in matlab
- If you downloaded it, thats fine. You will navigate to the data folder in matlab.
-
I missed the link to the file, can someone provide me the zip file?
-
how do you get those files to matlab?
- you will need to change to the folder you downloaded it to (e.g. by the address-line (e.g. Z:\ … above the editor <))
-
I got Error 404 page when I tried to download the .csv file.
-
please where is the link you showed for learning python?
-
where to get the .zip material, please?
-
can explain slowly how to get those files to matlab, please?
- You store them in your computer somewhere. Then you navigate to them within matlab, like with e.g. windows explorer (Default View , left side.)
- tried, but couldn't do it?
- where did you store the files?
- to the desktop
- Are you on windows? If you are, you can probably write ('C:\Users\yourusername\Desktop' into the address line (above the editor on default view, and you should see them))
-
I also was not able to get the files in the matlab.
- Where did you save them to? Essentially, the Matlab browser works the same way as the windows explorer, so you should be able to navigate to them.
-
I have studied the MATLAB Onramp before. Is it still necessary to take our course?
- If you didn't have any questions, you will probably not learn a lot new things here.
- OK. Thanks a lot!
- Thanks for the Phyton link
-
I don't see .m file in the folder
- don't worry, that file is not yet created. We will create it later.
BREAK TILL XX:00
We might have people in other time zones, that's why we put in XX.
- I am not from Aalto, can I get the credit anyway?
- Yes, we will provice a certificate that you can give to your studies' supervisor to be converted into 1 ECTS.
- i have column 1 through 29 not through 12 as you have
i have already taken the Matlab Fundamentals since last year
Working With Variables
Questions
-
Just as a remark: weight_lb / weight_kg are just names, they could named as something completely different, but it always makes sense to have your variable names telling what they contain. And you can simply autocomplete variables (writing the first few characters and pressing tab)
-
do the numbers of the matrix have to have space? can you do as in Python? ok thanks
- no, you can let out the spaces, but it makes code a lot less readable (same as in python).
-
I got >> patient_data = readmatrix('data/inflammation-01.csv');
Undefined function or variable 'readmatrix'.
- Are you on 2021b ? The function was introduced in 2019a. You could try
csvread()
instead.
-
Undefined function or variable 'readmatrix'. I am having this error
- see below what matlab version are you on?
- 2018b
- readMatrix was introduced in 2019a. You could try to use
csvread()
- ok!
- Load, csvread is also not working
- I typed patient_data = csvread('data/inflammation-01.csv')
- Are you on windows? If, you would have to change
csvread('data/inflammation-01.csv')
to csvread('data\inflammation-01.csv')
, csvread is OS sensitive, readmatrix autmatically converts / and \ depending on OS.
- Remark: This was also fixed probably in 2019a, when readmatrix was introduced, but older versions might still have this.
-
error in version 2017
I can't because I'm still out of office
patient_data = readmatrix('data/inflammation-01.csv')
Undefined function or variable 'readmatrix'.
- Ok, I have 2018a. Will get a newer version. Had to have this one in order to run some older scripts.
- As mentioned above: try using
csvread()
- yes that wor
-
for me csvread with \ or / doesn' work
- what is the result when you type
ls
- matlab-novice-inflammation
- than
cd('matalab-novice-inflammation')
(potentially twice), and try it again.
-
For those on Versions Prior to 2019a: You could try using 'csvread()'
- Thanks that worked
- csv read worked
-
when i type dir, it doesn't show two folders
-
it just show main folder again
-
what is the advantage to using readmatrix/csvread if load works just as well?
- readmatrix has better compatability with different OS. Not sure about load, but load is supposed to be used for loadiing .mat files (i.e. workspace variable files). Have a look at the matlab documentation about csvread under Compatability considerations
- OK!
Arrays
Questions
-
disp did not work for 2018a
-
Is there no way to convert the indexing to start from 0?
- No. It's in the language. You can do things like using < instead of <= when looping (introduced later), but indices in matlab will always start from 1, index 0 is not allowed and throws an error.
- Ok, I see.
- Admittedly, you could build something that is 0-based, but that's quite advanced, and you actually wouldn't want to do it, because you would be breaking language concepts, which is always just asking for trouble later on.
- Sounds like something I would not want to do. :)
- yep
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
I have another course at 14:15, should I mark my presence somewhere or is it fine if I just watch the rest in video?
- watching the video is fine.
Exercise
http://swcarpentry.github.io/matlab-novice-inflammation/02-arrays/index.html (scroll down to slicing)
- gen oye xyge
- gen oye xyge
- element(:) will display the data in one column, the resulting array has size 6x1 while the original array has size 1x6. M(:) results in matrix with size 64x1, which also display the data in single column.
- gen, oye, xyge, element(:) displays oxygen in one column. M(:) displays the entire matrix in one column, the colon operator seems to display an entire matrix or data set in only one column. size(element) is 1 6 and size(element(:)) is 6 1
- gen,goye,xyge
gen, oye, xyge
- gen, oye, xyge
- gen, oye, xyge - 1 6 / 6 1 - column under column
gen, oye, xyge
when M(:) it becomes array with one colum
Break till XX:00
Plotting data
Questions
Remark: When a command doesn't work please always provide the error message.
Note: You don't need to mark your presence for this course.
-
I had trouble with creating the patient_data, I got this error message:"Error using readmatrix (line 158)
Unable to find or open 'data/inflammation-01.csv'. Check the path
and filename or file permissions." even though I can clearly see that the folder "data" exists under matalab-novice-inflammation.
- Can you type
dir
where you are and see the "data" folder? What is the output of `pwd´?
- The output of pwd is 'C:\Documents\MATLAB\matlab-novice-inflammation', the output of 'dir' is . matlab-novice-inflammation
- than
cd('matalab-novice-inflammation')
, and try it again.
there are 2 nested folders with name "matlab-novice…" so you need to cd
again
-Ok, now it worked, thank you.
-
My 2018a runs only the last one when pressing enter
- What exactly do you mean with "only the last one"?
- I get an empty plot with y-label patients
- try running
hold on
after the imagesc
command. That might help.
-
- I meant in the next line. Sry, for the missunderstanding
- Ok, you never know what newbies think :) so it's difficult to give instructionsI ju
- I just realized I need to hold shift when changing lines so it understands I will want to run them together. After that it worked without 'hold on'
Exercises till XX:35
Scroll down where it says "plots" https://swcarpentry.github.io/matlab-novice-inflammation/03-plotting/index.html
Questions
- How can I not plot the line
- if I want put titles afterwards for two plots how i control which title goes to which plot?
- you run the
subplot
command again for the plot you want to modify
Feedback of the day
Good to write something positive and something which can be improved
One thing you liked
- This was excellent, I like it that you do it slowly - it is always possible to watch the video and speed it up
- Thank you, interesting!
- good pace :) and if you fell behind you could always follow the instructions
One thing we should improve
- A few things I realized that were maybe mentioned: when pressing enter and it opens the plot, you should not close it if you want to add title and axis labes - I was wondering (above) why I only got empty plots with titles and axes; also changing lines with shift+enter and then pressing enter only at the end worked (writing this so you realise how "stupid" problems beginners get) - so maybe going through things like enter and shift+enter is necessary
- can you please bit go slow next time I couldn't follow anything in the beginning
- Voice could still be a bit louder?
- Thanks and see you on Wednesday
Day 2
Plan for today
- Let's refresh the matlab IDE, the command window, and let's introduce the editor
- Refresh what we did on Monday and continue with https://swcarpentry.github.io/matlab-novice-inflammation/
Video from Day1 at https://www.youtube.com/watch?v=MvrLgyF_QPM . We will make a playlist and the Table of Content at some point. You can follow our YouTube channel for similar content: https://www.youtube.com/channel/UCNErdFO1_GzSkDx0bLKWXOA
Icebreaker Day 2:
What will you (or are you planning to) do with matlab in your studies/work?
- I like to learn use matlab for any matrix operations in addition to using julia.
- I may need it for som analyses or handling som data. I also tried do some heatmaps today, but I did not manage do them so that four maps share same heatmap legend
- Analyze data from the experiments. Maybe some embedded stuff with the actual experiment tools.
- I was able to do already Cholensky factorization! (meaning I'm just learning the basics to use with matrices)
- Take measurements of samples in a diluion refrigerator. Control network analyser and analyse data.
Questions
Remark: you can have multiple versions of Matlab installed in parallel on a machine, i.e. if you need a specific matlab version for some project/tool you can still have a newer version installed side-by-side, at least on Linux systems, but I think also on Windows systems
-
In my version readmatrix does not work, I use load() instead
- As a word of caution:
load
will only work with numeric data, as soon as you have text in that file it will either fail or produce odd results (not sure which it is at the moment).
-
what was the alternative for older versions? readcsv or something
-
does Matlab have some Undo button. In editor
- depends on what you want to undo. If you run a command: no. If you want to undo an edit in the editor: yes. (the same keyboard shortcut as in most progarms:
ctrl+z
/ ctrl+y
for undo/redo)
-
It seems that in version 2017 I have save the editor file first
Quick poll
patient_data = readmatrix('data/inflammation-01.csv')
- I can load the data: ooooooooooooooo
- I cannot load the data:
- I am just watching: o
- I can load the data
Writing Matlab Scripts
Questions
Remark: There are never too many comments in a file. It's better to have too much explanation than too little. (which does not mean that it is necessary to comment every c= a+b
line)
Remark: If you change your OS (or need to work on a machine with a different OS) and want to keep your keyboard shortcuts: Under Home -> Preferences ->Keyboard -> Shortcuts, you can select all "default" versions for different OS
- So is it %% that defines the sections
- yes, %% indicates this is a new section, while % only indicates its a commented line. This is only relevant in scripts, which can be run in sections, but for functions or other code, %% and % are the same.
- what was the save command
- What do you mean? The
save('File.mat')
command, will store your current workspace into the given mat file. If you provide additional arguments to that function (like save('file.mat','patient_data')
it will only write the specified variables to the file)
- the trouble is that while writing questions Enrico is going ahead and I did not have the time to copy the command from his editor
- and now I am dropping from the next section…
- The "testing the editor" part is not that important. If you just copy what's currently there, that's fine, the other was jus a bit of how the editor (and in particular sections) works.
- Ok. Would be nice if whatever need to be copied would be visible all the time - eg now the plot script is hidden by the browser
- Almost all code that needs to be copied is available on the software-carpentry site
- I know but it's hard to follow if I need to jump across different windows all the time. I already have zoom, this, and matlab side by side…
- admittedly, other than 2 monitors, I don't have a good suggestion here.
- I have two. Never mind… :)
- Yes, much better!
BREAK Till XX:00
Here the code for testing_the_editor.m
Writing Matlab Scripts cont'd
Questions
- The sound is lagging very much. Is it just me?
Repeating With Loops
Questions
- Does the command 1:4 for example, create a list of 1,2,3,4 with commas, or is it a vector. What type of object is it?
- Matlab mostly does not do objects. And almost everything is a type of array (number or cell array). So 1:4 created an array of length 4 with elements 1,2,3,4.
1:4
is a shorthand for: 1:1:4
and could also be written as: [1,2,3,4]. It's not like python where almost anything is an object. You can get the type
of a matlab variable by calling type(variablename)
, but at least for "simple" scripts, you will commonly only use double and char (possibly some strings nowadays).
- This can also be seen well by the true/false examples. where an array is
Exercise until 13:23
https://swcarpentry.github.io/matlab-novice-inflammation/05-loops/index.html scroll to where it says "Perform Exponentiation"
Write here when you are done.
- No, too little time for a beginner
- I almost got it. I did b = bb instead of c = cb
- Failed to write the formula correctly, multiply with n instead of b :(
- I have had problems with loops since I started use python, is there any simple way to learn it?
- What is your trouble with loops? Concept?
- I really need think it trough when I am writing one, I think its the in this excercise the 'i' variable that I don't still fully get. The variable after 'for'
- In this example (i.e. the software carpentry one), the
i
is only a helper variable, since it is not directly used in the loop. We just need to repeat the specified command 4 times, and loops in general need a "looping variable" or a condition to indicate, when they stop/how often they are executed. most of the times, the loop variable (i.e. the i
here) is used to e.g. indicate an index or something similar, but here it is esentially useless, and only needed for syntax reasons.
- thank you that makes it bit clearer :)
- Just done the aluminium one. Was easier than the exponent one, I think the maths was the hard part of that one.
- No need for math
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Done, too!
- Do the one for reverse looping :)
- Done
Exercises until 13:34, try doing the "Incrementing with Loops" and if you did it already try the next one "Looping in Reverse"
Repeating With Loops cont'd
Questions
- does not work in my 2017 version?
- its length not lenght
- oh that happens :)
- But it should have given you an error "Unecognized function or variable 'length'", which is probably a good hint to there being a typo.
- It just gave me error in line "for i = 1:lenght(word)". It did not tell where the error was
- Look a line above that. The first red text.
- Oh there it is thanks. It seems I can't read…
- Trust me, you are not the only person who has trouble reading error messages
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
It comes with experience in programming.
Remark: Error message are commonly structured as follows:
First line: The actual error that occured
Everything else: The so called "stack-trace", i.e. what command led to this error occuring
The last is most likely the command you used, the furthest up is the file/function where it occured
BREAK Till XX:00
- awake, but going slowly
- I see some hope in learning loops, but this is going too fast for me at the moment… It's always with coding courses that the whole thing about indexing and how things refer to each other is somewhat difficult for me and that is where I get lost. But this is a good course!
- intrested :D. This has much better pace than other web courses I have been
Repeating With Loops cont'd (2)
Questions
Remark for subplot: Subplot essentially only divides the are of the figure into a grid. That means you can change the size of the grid at any time and plot e.g. 2 above one centered plot, by using the following code:
here, the first two plots are put into positions 1 and 2 of a 2x2 grid, while the third plot is put into the area provided by positions 6 and 7 of a 2x4 grid
-
Q: Why we need double equal mark "num == 0"?
- Good Question and an important syntax element. If you had
num = 0
, you would assign 0 to num
. Therefore there needs to be a distinction between equality comparison (==
) and assgignment =
. This is the same in all languages I know of (and which are in use).
- OK got it, thanks!
-
https://se.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html
-
Question. After this course is it possible to somehow return to the course materials? eg. like https://swcarpentry.github.io/matlab-novice-inflammation/06-cond/index.html. I mean just for instructions how to use Matlab? I allready did the Onramp
- The material should be online after the course. the softwarecarpentry course might change at some point but I don't expect it to disappear.
-
To get the credits the unique homework is to complete the course of Fundamental matlab?
Feedback for today
Write something positive here
- This is a very nice course, and thanks for changing the window set up at the first break!
- I thought the pace was good today, and pretty easy to follow.
- .
- I like that this course is actually made for beginners and even I could follow, even though the looping was a little tricky
- .
Write something that could be improved
- Maybe go through a bit more of what refers to where and what each line would be written out (as comments?) So e.g. n=1:length(word) %This line gives indexes to the letters from 1 to the end of the word etc
- Some of the learning material could be maybe a little more precise, I mean it could be explained better why certain commands are used or what the do. They are mostly very clear though
- .
- .