Python for MATLAB Development - Extend MATLAB with 300,000+ Modules from the Python Package Index

This book shows you how to enhance MATLAB with Python solutions to a vast array of computational problems in science, engineering, optimization, statistics, finance, and simulation. It is three books in one: A thorough Python tutorial that leverages your existing MATLAB knowledge with a comprehensiv...

Full description

Saved in:
Bibliographic Details
Main Author Danial, Albert
Format eBook
LanguageEnglish
Published Berkeley, CA Apress, an imprint of Springer Nature 2022
Apress
Apress L. P
Edition1
Subjects
Online AccessGet full text
ISBN9781484272220
1484272226
1484272234
9781484272237
DOI10.1007/978-1-4842-7223-7

Cover

Table of Contents:
  • Title Page Preface Table of Contents 1. Introduction 2. Installation 3. Language Basics 4. Data Containers 5. Dates and Times 6. Call Python Functions from MATLAB 7. Input and Output 8. Interacting with the File System 9. Interacting with the Operating System and External Executables 10. Object-Oriented Programming 11. NumPy and SciPy 12. Plotting 13. Tables and Dataframes 14. High Performance Computing 15. Language Pitfalls Appendices Index
  • 7.1.6.2 XML with Namespaces -- 7.1.6.3 Writing XML -- 7.1.6.4 Pretty-Printing XML -- 7.1.7 YAML -- 7.1.8 JSON -- 7.1.9 ini -- 7.2 Recipe 7-1: Read YAML Files -- 7.3 Recipe 7-2: Write YAML Files -- 7.4 Recipe 7-3: Read an ini File -- 7.5 Recipe 7-4: Write an ini File -- 7.6 Binary Files -- 7.7 Excel .xls, .xlsx -- 7.7.1 Reading .xls and .xlsx Files -- 7.7.1.1 Get Worksheet Names -- 7.7.1.2 Get a Worksheet's Size -- 7.7.1.3 Extract Rows -- 7.7.1.4 Extract Columns -- 7.7.1.5 Extract a Rectangular Block -- 7.7.2 Writing .xlsx Files -- 7.8 Recipe 7-5: Write an .xlsx File -- 7.9 HDF5 -- 7.9.1 Reading an HDF5 File -- 7.9.2 Writing an HDF5 File -- 7.9.2.1 Abbreviated Writing Notation -- 7.9.2.2 Beware: MATLAB's h5read(), h5write() Transpose Matrices -- 7.9.3 Reading and Writing HDF5 Dataset Attributes -- 7.9.4 Iterating over All HDF5 Datasets -- 7.10 NetCDF4 -- 7.10.1 Reading a NetCDF4 File -- 7.10.1.1 Reading NetCDF4 Attributes -- 7.11 SQLite -- 7.12 Recipe 7-6: CRUD with an SQLite Database -- 7.13 Pickle Files -- 7.14 MATLAB .mat Files -- 7.14.1 Inspecting the Contents of a .mat File -- 7.14.2 Reading a .mat File -- 7.14.3 Writing a .mat File -- 7.14.3.1 Writing Cell Arrays -- 7.14.3.2 Writing an Array of Structs -- 7.14.4 mat Version 7.3 -- 7.15 Command-Line Input -- 7.15.1 Python: sys.argv -- 7.15.2 MATLAB: Function Arguments -- varargin -- 7.15.3 Python: argparse -- 7.16 Interactive Input -- 7.17 Receiving and Sending over a Network -- 7.17.1 HTTP, HTTPS -- 7.17.2 Python As a Web Server -- 7.17.3 TCP/IP -- 7.17.3.1 Python Client -- 7.17.3.2 Python Server -- 7.17.3.3 MATLAB Client -- 7.17.3.4 MATLAB Server -- 7.18 Recipe 7-7: TCP Server -- 7.19 Interacting with Databases -- 7.19.1 PostgreSQL -- 7.19.2 MongoDB -- 7.20 Recipe 7-8: CRUD with a PostgreSQL Database -- 7.21 Recipe 7-9: CRUD with a MongoDB Database -- 7.21.1 Read -- 7.21.2 Update
  • 4.3.11 Indexing Nested Containers -- 4.3.12 Membership Test: Does an Item Exist in a List? -- 4.3.13 Find the Index of an Item -- 4.3.14 Apply an Operation to All Items (List Comprehension) -- 4.3.15 Select a Subset of Items Based on a Condition -- 4.3.16 How Many Times Does an Item Occur? -- 4.3.17 Remove the First or Last (or Any Intermediate) List Item -- 4.3.18 Remove an Item by Value -- 4.3.19 Merging Multiple Lists -- 4.3.20 Unmerging Combined Lists -- 4.3.21 Sort a List -- 4.3.22 Reverse a List -- 4.4 Python Tuples -- 4.5 Python Sets and MATLAB Set Operations -- 4.6 Python Dictionaries and MATLAB Maps -- 4.6.1 Iterating over Keys -- 4.6.2 Testing for Key Existence -- 4.6.2.1 get() and .setdefault() -- 4.6.2.2 Key Collision -- 4.6.3 Iterating over Keys, Sorting by Key -- 4.6.4 Iterating over Keys, Sorting by Value -- 4.6.4.1 Secondary Sorts -- 4.6.5 Tuples As Keys -- 4.6.6 List Values -- 4.7 Structured Data -- 4.7.1 Method 1: namedtuple -- 4.7.2 Method 2: SimpleNamespace -- 4.7.3 Method 3: Classes -- 4.7.4 Method 4: Data Classes -- 4.7.4.1 Field Values -- 4.7.4.2 Relationships Between Dependent Data Classes -- 4.7.4.3 Dynamic Modification of Data Classes -- 4.7.4.4 Traversing Linked Data Classes -- 4.7.4.5 Type Validation with Pydantic -- 4.7.5 Enumerations -- 4.8 Caveat: "=" Copies a Reference for Nonscalars! -- Chapter 5: Dates and Times -- 5.1 Time -- 5.1.1 Current Time -- 5.1.2 Time String Formats -- 5.1.3 tic, toc -- %timeit -- 5.2 Dates -- 5.2.1 datetime Objects to and from Strings -- 5.2.2 Time Deltas -- 5.3 Timezones -- 5.3.1 UTC vs. Local Time -- 5.4 Time Conversions to and from datetime Objects -- 5.4.1 Unix Epoch Seconds -- 5.4.2 ISO 8601 Time String -- 5.4.3 Julian Date -- Modified Julian Date -- GPS Time -- 5.5 zoneinfo in Python &gt -- = 3.9 -- 5.5.1 List Available Timezones
  • Intro -- Table of Contents -- About the Author -- About the Technical Reviewers -- Preface -- Acknowledgments -- Chapter 1: Introduction -- 1.1 Learn Python Through MATLAB Equivalents -- 1.2 Is Python Really Free? -- 1.3 What About Toolboxes? -- 1.4 Why Python Won't Replace MATLAB -- 1.5 Contents at a Glance -- 1.6 I Already Know Python. How Do I Call Python Functions in MATLAB? -- 1.7 The Recipes Don't Work! MATLAB Crashes! (and What to Do About It) -- Chapter 2: Installation -- 2.1 Downloads -- 2.1.1 Match Your Python and MATLAB Versions! -- 2.1.2 Verify That Python Runs -- 2.2 Post-Install Configuration and Checkout -- 2.3 Creating and Running a Python Program -- 2.4 The Curse of Choice -- 2.5 Virtual Environments -- 2.5.1 matpy, the Virtual Environment Used in This Book -- 2.5.2 Commands to Manage Virtual Environments -- 2.5.3 Keeping Your Virtual Environment Current -- 2.6 ipython, IDEs -- 2.6.1 Autoload Modules When ipython Starts -- 2.7 Python and MATLAB Versions Used in This Book -- Chapter 3: Language Basics -- 3.1 Assignment -- 3.1.1 Assignment with = -- 3.1.2 In-Place Updates with +=, -=, and Others -- 3.1.3 Walrus Operator,:= -- 3.2 Printing -- 3.3 Indentation -- 3.3.1 Tabs -- 3.4 Indexing -- 3.4.1 Brackets vs. Parentheses -- 3.4.2 Zero-Based Indexing and Index Ranges -- 3.4.3 Start, End, and Negative Indices -- 3.4.4 Index Strides -- 3.4.5 Index Chaining -- 3.5 for loops -- 3.5.1 Early Loop Exits -- 3.5.2 Exit from Nested Loops -- 3.6 while Loops -- 3.7 if Statements -- 3.7.1 Boolean Expressions and Operators -- 3.7.2 Range Tests -- 3.8 Functions -- 3.8.1 Pass by Value and Pass by Reference -- 3.8.1.1 Scalars Are Passed by Value -- 3.8.1.2 Lists, Dicts, and Arrays Are Passed by Reference -- 3.8.1.3 List, Dict, and Array Contents Can Be Replaced in Their Entirety -- 3.8.2 Variable Arguments -- 3.8.3 Keyword Arguments
  • 5.5.2 Date Increments Across Daylight Savings Transition -- 5.6 References -- Chapter 6: Call Python Functions from MATLAB -- 6.1 Configure MATLAB to Recognize Python -- 6.2 Does It Work? -- 6.3 Importing (and Reloading) Python Modules -- 6.4 Configure startup.m for Python Work -- 6.5 Create Python Variables and Call Python Functions in MATLAB -- 6.5.1 Scalars -- 6.5.2 Lists and Cell Arrays -- 6.5.3 Tuples -- 6.5.4 Numeric Arrays -- 6.5.5 Dictionaries and Structs -- 6.5.6 Keyword Arguments -- 6.5.7 Python-to-MATLAB and MATLAB-to-Python Variable Converters -- 6.5.8 Traversing Generators -- 6.5.9 Traversing zip() -- 6.6 Modifying the Python Search Path Within MATLAB -- 6.6.1 Extending sys.path with an Alias -- 6.6.2 Extending sys.path with insert() -- 6.6.3 Extending sys.path with append() -- 6.7 Python Bridge Modules -- 6.8 Debugging Python Code Called by MATLAB -- 6.9 Summary of Steps to Calling Python Code from MATLAB -- 6.10 Call MATLAB from Python -- 6.10.1 Install matlab.engine -- 6.10.2 Call Functions in a New MATLAB Session -- 6.10.3 Call Functions in an Existing MATLAB Session -- 6.11 Other Mechanisms for MATLAB/Python Interaction -- 6.11.1 System Calls and File I/O -- 6.11.1.1 JSON -- 6.11.1.2 MATLAB to Python via JSON -- 6.11.1.3 Python to MATLAB via JSON -- 6.11.2 TCP/IP Exchange -- Chapter 7: Input and Output -- 7.1 Text Files -- 7.1.1 Reading Corrupted Text Files with pathlib -- 7.1.2 Reading and Writing Numeric Data -- 7.1.3 I/O Exceptions -- 7.1.4 Parsing Text -- 7.1.4.1 Stripping Whitespace -- 7.1.4.2 Splitting a String with a Substring -- 7.1.4.3 Regular Expressions -- 7.1.4.4 Splitting a String with a Regex -- 7.1.4.5 Searching a String for Patterns -- 7.1.5 csv -- 7.1.5.1 Python's csv Module -- 7.1.5.2 Pandas's read csv() Function -- 7.1.5.3 NumPy's genfromtxt() Function -- 7.1.6 XML -- 7.1.6.1 XML Without Namespaces
  • 7.21.3 Delete
  • 3.8.4 Decorators -- 3.8.5 Type Annotation and Argument Validation -- 3.8.6 Left-Hand Side Argument Count -- 3.9 Generators -- 3.9.1 yield, next() -- 3.9.2 range() -- 3.10 Scoping Rules and Global Variables -- 3.11 Comments -- 3.11.1 Docstrings -- 3.12 Line Continuation -- 3.13 Exceptions -- 3.14 Modules and Packages -- 3.14.1 Namespace -- 3.14.1.1 import X, or import X as Y -- 3.14.1.2 from X import Y -- 3.14.1.3 from X import Y as Z -- 3.14.1.4 An Antipattern: from X import * -- 3.14.2 def main() -- 3.14.3 Module Search Path -- 3.14.4 Installing New Modules -- 3.14.5 Module Dependency Conflicts and Virtual Environments -- Chapter 4: Data Containers -- 4.1 NumPy Arrays -- 4.2 Strings -- 4.2.1 Strings, Character Arrays, and Byte Arrays -- 4.2.2 String Operations -- 4.2.2.1 String Length -- 4.2.2.2 Append to a String -- 4.2.2.3 Repeat a String -- 4.2.2.4 Convert to Upper- or Lowercase -- 4.2.2.5 Replace Characters -- 4.2.2.6 Method Chaining -- 4.2.3 Formatting -- 4.2.4 Separate a String into Words -- 4.2.5 Tests on Strings -- 4.2.5.1 Testing for Equality -- 4.2.5.2 Check Trailing Characters -- 4.2.5.3 Check Starting Characters -- 4.2.5.4 Do Given Characters Appear in a String? -- 4.2.6 String Searching, Replacing with Regular Expressions -- 4.2.6.1 Does a String Match a Regex? -- 4.2.6.2 Match a Regex and Capture Substrings -- 4.2.6.3 Replace Text Matching a Regex with Different Text -- 4.2.7 String Templates -- 4.3 Python Lists and MATLAB Cell Arrays -- 4.3.1 Initialize an Empty List -- 4.3.2 Create a List with Given Values -- 4.3.3 Get the Length of a List -- 4.3.4 Index a List Item -- 4.3.5 Extract a Range of Items -- 4.3.6 Warning-Python Index Ranges Are Not Checked! -- 4.3.7 Append an Item -- 4.3.8 Append Another List -- 4.3.9 Preallocate an Empty List -- 4.3.10 Insert to the Beginning (or Any Other Position) of a List