MATLAB Packages

This chapter introduces Packages in MATLAB, a namespace-based approach for organizing and managing code. Packages help prevent naming conflicts and make large codebases more maintainable, especially for collaborative projects.

Chapter Goal

The goal of this chapter is to provide a comprehensive understanding of MATLAB packages, focusing on:

  1. Learning how to create and use packages for code organization.
  2. Exploring the structure and syntax of MATLAB packages.
  3. Understanding the benefits of namespace management.
  4. Implementing best practices for package creation and use.

Key Characteristics for MATLAB Packages

  1. Namespace Management: Packages define unique namespaces, preventing naming conflicts.
  2. Hierarchical Structure: Support for nested packages to organize code hierarchically.
  3. Encapsulation: Provides a mechanism for encapsulating related functions, classes, and data.
  4. Reusability: Facilitates code reuse across projects by providing well-defined structures.
  5. Integration: Seamless integration with MATLAB’s folder and file management.

Basic Rules for MATLAB Packages

  1. Folder Naming: Use a folder name prefixed with + to define a package (e.g., +mypackage).
  2. Access Functions and Classes: Use dot notation to access package members (e.g., mypackage.myfunction).
  3. Nested Packages: Define subfolders with + prefix for nested packages.
  4. Avoid Conflicts: Ensure package names are unique and do not conflict with existing MATLAB functions.
  5. Document Members: Provide clear documentation for all package members.

Best Practices

  1. Organize Related Code: Group related functions and classes within a single package.
  2. Use Descriptive Names: Choose meaningful and descriptive names for packages and members.
  3. Document Namespace Usage: Clearly document the purpose and usage of the package.
  4. Limit Member Scope: Use access modifiers to restrict the scope of classes and methods where appropriate.
  5. Test Packages: Write unit tests for package members to ensure correctness and reliability.

This chapter equips you with the knowledge to effectively use MATLAB packages for organizing and managing code, enabling scalable and maintainable software development. Subsequent sections will delve into syntax, examples, and real-world applications.

Syntax Table

Serial No Component Syntax Example Description
1 Define a Package Folder mkdir(‘+mypackage’); Creates a folder named +mypackage, defining a MATLAB package.
2 Add Functions to Package function result = myfunction(args) Adds a function myfunction inside the package folder.
3 Access Package Function mypackage.myfunction(args); Accesses and runs the function myfunction within the mypackage namespace.
4 Nested Package Folder mkdir(‘+mypackage/+subpkg’); Creates a nested package subpkg within the parent package mypackage.
5 Document Package Members % Add comments at the top of functions Documents the purpose and usage of package members for better readability.

Syntax Explanation

1. Define a Package Folder

What is Defining a Package Folder?

Creating a package folder involves naming a folder with a + prefix, signaling MATLAB that it is a package.

Syntax:

mkdir(‘+mypackage’);

Detailed Explanation:

  • The +mypackage folder acts as a namespace container.
  • All functions and classes inside this folder belong to the mypackage namespace.

Example:

mkdir(‘+mypackage’);

cd(‘+mypackage’);

2. Add Functions to Package

What is Adding Functions to a Package?

Adding functions to a package involves placing .m files in the package folder.

Syntax:

function result = myfunction(args)

    % Function code

end

Detailed Explanation:

  • Save the file as myfunction.m inside the +mypackage folder.
  • The function is now accessible as mypackage.myfunction.

Example:

% File: +mypackage/myfunction.m

function result = myfunction(x)

    result = x^2;

end

3. Access Package Function

What is Accessing a Package Function?

Accessing functions in a package uses dot notation, combining the package name and function name.

Syntax:

mypackage.myfunction(args);

Detailed Explanation:

  • Call the function using the package name as a prefix to ensure proper namespace usage.

Example:

result = mypackage.myfunction(5);

disp(result);

4. Nested Package Folder

What is a Nested Package Folder?

Nested package folders allow hierarchical organization of code within sub-namespaces.

Syntax:

mkdir(‘+mypackage/+subpkg’);

Detailed Explanation:

  • Create a folder with a + prefix inside an existing package folder.
  • Functions in the nested folder belong to the sub-namespace.

Example:

% File: +mypackage/+subpkg/subfunction.m

function result = subfunction(y)

    result = y^3;

end

output = mypackage.subpkg.subfunction(3);

disp(output);

5. Document Package Members

What is Documenting Package Members?

Documenting functions and classes within a package ensures clarity and better usability.

Syntax:

% Add comments at the top of each function or class file

Detailed Explanation:

  • Use comments to describe the purpose, inputs, and outputs of package members.

Example:

% File: +mypackage/myfunction.m

% myfunction computes the square of a number.

function result = myfunction(x)

    result = x^2;

end

Notes

  • Package folders must be on the MATLAB path to use their contents.
  • Use nested packages for organizing large codebases effectively.

Warnings

  • Avoid naming conflicts by ensuring package names are unique.
  • Do not modify package folder names without updating all references.

Real-Life Project: Building a Scientific Calculator Package

Project Name: Modular Scientific Calculator

Project Goal:

To design and implement a MATLAB package for performing modular scientific calculations using a namespace-based structure.

Steps in the Project:

  1. Define the Package Folder:
    • Create a folder named +calculator to define the package namespace.
  2. Add Mathematical Functions:
    • Implement basic functions such as addition, subtraction, multiplication, and division within the +calculator package.
  3. Create Advanced Functions:
    • Include advanced operations like trigonometry, logarithms, and power functions in a nested package +calculator/+advanced.
  4. Document Package Contents:
    • Provide clear documentation for each function, explaining its purpose and usage.
  5. Test the Package:
    • Write scripts to test the package and ensure all functions work correctly.

Code for This Project:

% Step 1: Define package folder structure

mkdir(‘+calculator’);

mkdir(‘+calculator/+advanced’);

 

% Step 2: Add basic mathematical functions

% File: +calculator/add.m

function result = add(a, b)

    result = a + b;

end

 

% File: +calculator/subtract.m

function result = subtract(a, b)

    result = a – b;

end

 

% Step 3: Add advanced functions

% File: +calculator/+advanced/power.m

function result = power(base, exponent)

    result = base^exponent;

end

 

% File: +calculator/+advanced/sin.m

function result = sinValue(angle)

    result = sin(angle);

end

 

% Step 4: Test the package

% File: test_calculator.m

result1 = calculator.add(5, 3);

result2 = calculator.advanced.power(2, 3);

result3 = calculator.advanced.sin(pi/2);

disp([‘Addition Result: ‘, num2str(result1)]);

disp([‘Power Result: ‘, num2str(result2)]);

disp([‘Sine Result: ‘, num2str(result3)]);

Save and Run:

  1. Save the package files in the respective directories.
  2. Run test_calculator.m to test the package.

Expected Output:

  1. Console Output:
    • Results of basic and advanced calculations:
      • Addition Result: 8
      • Power Result: 8
      • Sine Result: 1

Learning Outcomes:

  • Understand how to create and organize MATLAB packages.
  • Learn to implement hierarchical namespaces for modular code.
  • Gain experience in testing and documenting reusable code modules.