---
# System prepended metadata

title: Welcome to Matlab Basics 2021
tags: [Matlab_Basics]

---

# Welcome to Matlab Basics 2021
###### tags: `Matlab_Basics`
:::info
This is an online course.
- Course page + repository + info: https://version.aalto.fi/gitlab/eglerean/matlabcourse/-/tree/master/AY20212022/MatlabBasics2021
- This document: https://hackmd.io/@AaltoSciComp/MatlabBasics2021
- Archive of this document (Day1 and 2): https://hackmd.io/@AaltoSciComp/Matlab_Archive
- Learning materials: https://swcarpentry.github.io/matlab-novice-inflammation/
- Dates: 22, 24, 26 November, 11:50-15:00 EET (Finnish time)
- Zoom room: SENT VIA EMAIL
- Course prerequisites:
    - Have a working copy of the most recent Matlab on your machine, or on a remote system that you can comfortably use.
    - Create an account at https://matlabacademy.mathworks.com/ using your institution email address to access extra content
 - Please note that we will record the zoom: do not switch your camera or microphone unless you intend to.
 - Video from Day1 at https://www.youtube.com/watch?v=MvrLgyF_QPM 
 - You can follow our YouTube channel for similar content: https://www.youtube.com/channel/UCNErdFO1_GzSkDx0bLKWXOA
:::

### Questions from the registration form
- Can we get recording of those lectures as well?
    - We will record the course, however this will be almost identical to what we did last year, which is already available on YouTube at https://www.youtube.com/watch?v=gbvtrJFWitU&list=PLZLVmS9rf3nORXjlO26n7Iov7i_a-s2Qs so feel free to watch those if you cannot attend to the live sessions

- Can I get a certificate / credit for the course?
    - To get a certificate or credit, you need to complete 100% of "Matlab Fundamentals" from Matlab Academy and send a link to the Matlab Academy certificate https://matlabacademy.mathworks.com/details/matlab-fundamentals/mlbe
- 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?
    - Unfortunately there is not enough time to cover Simulink, the best is to self learn it at: https://matlabacademy.mathworks.com/#simulink


# Day 3

## Icebreaker

There are many toolboxes that are developed by other users (i.e. not made by matlab). Using your favourite search engine, can you search for a matlab toolbox in your field and paste the link here below and a tiny description? (here what other learners did last year https://hackmd.io/IG9SGM5AQia48IwKp_Hnjg?view#Icebreaker-day-2)

- Toolbox to work with brain images https://www.mathworks.com/matlabcentral/fileexchange/8797-tools-for-nifti-and-analyze-image 
- Brain connectivity toolbox https://sites.google.com/site/bctnet/
- I used to use the [COBRA Toolbox](https://github.com/opencobra/cobratoolbox) constraint based metabolic modelling) .
- Quantum Optics Toolbox https://qo.phy.auckland.ac.nz/toolbox/
- .

### Questions

- Could you show again the tools found from last year?
    - https://hackmd.io/IG9SGM5AQia48IwKp_Hnjg?view#Icebreaker-day-2

## Recap of Day1/2

## Making Choices cont'd

### Questions
**Remark: when using `if` clauses, try to have an explicit statement like (`if any(vectorA)`) instead of `vectorA`. It is a, more easily readable, and you won't run into the borderline situations shown in the lecture material. I.e. make clear what exactly you are testing, since otherwise bugs are much more difficult to find.**

### Exercise: Nesting
https://swcarpentry.github.io/matlab-novice-inflammation/06-cond/index.html

#### Results:
- 1a, 2a, 3a, 1b, 2b, 3b
- 1a,2a, 3a, 1b, 2b....
- 1a,2a,3a,1b,2b,3bdisp

#### Code snippet (before adding if statements)
```(matlab)
%PLOT_ALL	Developing code to automate inflammation analysis


% first we get a list of all the CSV files
files = dir('data/inflammation-*.csv');

for i = 1:length(files)
    % we loop through each file
    file_name = files(i).name;
    
    % Generate string for image name
    img_name = replace(file_name, '.csv', '.png');
    
    % Generate path to data files (input) and image file (output)
    file_name = fullfile('data',file_name);
    img_name = fullfile('results',img_name);
    
    % --- here goes the actual processing
    % 1. loading the patient data
    disp(['Loading data for: ' file_name])
    patient_data = readmatrix(file_name); % you can use csvread
    
    % 2. making the figure with subplots
    figure('visible', 'off')

    subplot(2, 2, 1)
    plot(mean(patient_data, 1))
    title('Average')
    ylabel('Inflammation')
    xlabel('Day')

    subplot(2, 2, 2)
    plot(max(patient_data, [], 1))
    title('Max')
    ylabel('Inflammation')
    xlabel('Day')

    subplot(2, 2, 3)
    plot(min(patient_data, [], 1))
    title('Min')
    ylabel('Inflammation')
    xlabel('Day')
    
    
    % 3. save the figure to a png file in img_name
    disp(['Saving figure in file: ' img_name])
    print(img_name, '-dpng')
    close()
end
```

### Questions
**Remark: If you select a larger for loop or if clause and press `ctrl+i`, matlab will auto-indent the selection. You can also auto-indent a whole file by selecting all (`ctrl+a`) and then pressing `ctrl+i` this is quite useful, since it creates a consistent way of indentation, which is useful for readability, it also ensures, that the same way for creating the indentation is used (and e.g. not a mix of tabs and spaces)**

## Creating Functions 

## Exercise until 13:20
https://swcarpentry.github.io/matlab-novice-inflammation/07-func/index.html
Scroll to "Concatenating in a function"
Hint: you will need to start with 

`edit fence.m`

Write here if you are done or need help
- done
- didn't understand even by looking at the solution
    - the idea is to put the variable passed in as "original" within copies of the "wrapper" variable. Both would need to be character-arrays (or strings). `strcat` concatenates (i.e. puts together) all str/char elements that are passed to it in the order that they are passed in. e.g. `strcat('I','am','here')` would result in `'Iamhere'`. so by putting `wrapper` both before and after `original`, the `original` will be "fenced" by `wrapper`. Does this make it clearer?
    - I need a little more time to go through it :D


## "Getting the Outside" exercise until 13:32

- Hah, I managed to do this once I muted Enrico :D
- I solved it :) but with array.. changed it more like the example

**Remark: Matlab strings are not accessible by indices liek char arrays are, i.e. if you have `str = "abcde"`, and use the `outer` function on this (e.g. outer(str)), you get "abcdeabcde" as a result, because matlab considers anything to be a vector, i.e. you can always access the first (and last) element of something. In this instance with only one element present, both the first and the last element of the the "string array" `str` are "abcde", which would get concatenated**

## Exercise until xx:10 

"Testing a function" scroll down in this page https://swcarpentry.github.io/matlab-novice-inflammation/07-func/index.html

If you were able to solve already part 1, write it here
- .I think it works fine :); input: [1, 5, 7, 90]; output: 0    0.0449    0.0674    1.0000
- I tested with z = linspace(24,156,10) and the answer was 0    0.1111    0.2222    0.3333    0.4444    0.5556    0.6667    0.7778    0.8889    1.0000 (I almost got the code right but I had to fix it with hints)
- . 


### Questions
- What is the difference at the hints that you use L = min(min(v)) vs. just L = min(v)?
    - not sure, what exactly you mean. min(min()) would result in the minimum of a 2-dimensional array. The problem with min (and many other functions) is that they work on one dimension of an array. i.e. if your input is of a higher dimensionality, you don't get a single value but a vector of (in this instance) minimums of each row if you don't use min(min(x)) or min(x(:)). Does this answer your question?

- Can you copy here the working functions, I think I have some error that I can't see. I would like have both plot_all_functional.m and plot_dataset.m
    - I will!
- Ok thanks
    - Did you have a look at the examples from SoftwareCarpentry (i.e. the solutions they provide?)
        - I don't have since we did different version than in the web
            - They are very similar, but we will post the code here later.
            - I debugged the code and found the error :) I had typo in file_name vs. filename...
    - hurra!
    - from experience, if you don't make typos, then there's something wrong :D I rarely have my code to work on the first run
 - ^_^
 
- How long will be the recording active?
    - I will switch it off soon
    I mean The lectures recorded in the link?
    - beginning of next week they should all be on youtube. They can stay there "forever" 
    - Excellent, thanks so much
    - 
    





# Final feedback 

What did you like about this course?
- .Nice course! what about the advanced level?
- .
- .
- .
- I liked this course, it had good pace

What could still be improved for future runs?
- .
- .
- .
- However, at the end there was bit hurry.
    - True, it was really just a small what steps make sense (i.e. looking at the documentation, putting breakpoints)

See you at Matlab Advanced May 2022 :) 
(past year https://hackmd.io/@eglerean/MatlabAdvanced2021)

---
:::info
**This is the end of the document, WRITE ABOVE THIS LINE ^^**

HackMD can feel slow if more than 100 participants are editing at the same time: If you do not need to write, please switch to "view mode" by clicking the eye icon on top left :eye:


