How does one typically implement a "Library"?

조회 수: 28 (최근 30일)
eng3
eng3 2025년 11월 6일 23:08
이동: Stephen23 2025년 11월 8일 4:44
I have several MATLAB projects that I reuse frequently. Often I want to take functions or classes from multiple projects and use them in a new one—essentially creating a shared "library" of code.
What’s the best practice for organizing and reusing code across projects?
These are the approaches I’ve considered, and their issues:
  • Copying code into the new project – easy at first, but hard to maintain when the original code changes.
  • Adding project folders to the MATLAB path – works, but can lead to name conflicts if different projects contain files/functions with the same name.
  • Namespace (+folder) packages – solves name conflicts and keeps things organized, but scripts inside a namespace can't automatically see other scripts in the same folder unless you prefix everything with the package name (e.g., package.func). This isn’t practical when dealing with large or third-party codebases.
Ideally, I want MATLAB to:
  • Let scripts call other scripts/functions located in the same folder without changing the current working directory, and
  • Avoid name conflicts across projects,
  • Without needing to rewrite existing code to add namespace prefixes everywhere.
This seems to be what toolboxes do.
How do people typically manage this in MATLAB? Is there a clean way to build a reusable code library from multiple projects without duplicating code or modifying everything to use package prefixes?

답변 (1개)

Taylor
Taylor 2025년 11월 7일 20:16
  댓글 수: 1
eng3
eng3 2025년 11월 7일 21:53
이동: Stephen23 2025년 11월 8일 4:44
No not really. Using "projects" is a way of build projects using external code but it's two fold. I some matlab projects to be easily accessible without having to worry about name conflicts and I'd like to be able to build projects using other projects without needing to modify their source. Something like a mix of namespace folders and addpath where I can access projects without adding to path, keeping the execution scoped interally to the component namespace folder strucuture.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

태그

제품


릴리스

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by