Matlab Notes_Scripts, Functions and Plots-學習記筆

Shih Jiun Lin Lv4

Matlab Notes-Scripts, Functions and Plots

Scripts and Functions

  • A script file called an m-file (with an extension .m), which is only a text file that contains each line of the file as though you typed them at the command prompt.

  • Creating a block of code is through subroutines. These are called functions, which also allow us to extend the capabilities of MATLAB. In fact a major portion of MATLAB is assembled using function files in several categories and using special collections called "toolboxes".

  • A major difference between script and function files is that the first executable line in a function file begins with the keyword function followed by an output-input variable declaration.

Applications of Plots with Functions and Scripts

Plotting a Sinusodial Wave of

  • Function

    1
    2
    3
    4
    5
    function plot_sin(n,t)
    x=sin(n*pi*t);
    plot(t, x, 'b'); %plot the graph with color "black"
    xlabel('t in sec'); ylabel('sin(n*pi*t)'); %setting labels for x and y axises
    title('plot of sin(n*pi*t)'); %setting title for the plot

  • Script

    1
    2
    t=0:0.01:1; n=2;
    plot_sin(n, t);

  • Result

Plotting a Discrete-Time Signal of

  • Function

    1
    2
    3
    4
    5
    6
    function plot_dt_sin(k,n)
    x=sin(k*pi*n);
    x_plot = stem(n, x, 'b', 'filled'); %stem plot
    set(x_plot, 'markersize', 4); %chagne the size of the circle
    xlabel('n'); ylabel('sin(k*pi*n)'); %setting labels for x and y axises
    title('plot of sin(k*pi*n)'); %setting title for the plot

  • Script

    1
    2
    n=0:1:20; k=0.1;
    plot_dt_sin(k, n);

  • Result

Creating Two or More Plots Simultaneously

  • We can do so by using . (link )

  • Basic Usage

    1
    2
    %Adding the followings line
    subplot(rows, columns, which row, order);

  • Result of subplot(2, 1, 1) and subplot(2, 1, 2)

  • Title: Matlab Notes_Scripts, Functions and Plots-學習記筆
  • Author: Shih Jiun Lin
  • Created at : 2024-02-26 11:00:47
  • Updated at : 2024-03-04 22:57:37
  • Link: https://shih-jiun-lin.github.io/2024/02/26/Matlab Notes-Scripts, Functions and Plots/
  • License: This work is licensed under CC BY-NC-SA 4.0.