📊

Module 6 Video - writematrix: Exporting Numeric Data to an Excel Workbook: Part 3

Mar 7, 2025

Lecture Notes: Expanding Options for WriteMatrix in MATLAB

Introduction

  • Objective: Learn how to use writeMode name-value pair to expand options of WriteMatrix in MATLAB.
  • File Used: w2.xlsx; recommended to create a copy w2mod.xlsx for modification.

Adding Data Below Existing Information

  • Challenge: Adding data below existing data requires knowing the lower left corner of the data range.
  • Solution: Use writeMode with the value append.
    • MATLAB identifies lower left corner and writes new data starting below this point.
    • Used range: Smallest rectangle encompassing all data in the worksheet.

Example 1: Sheet Test1

  • Matrices Used: XMAT4
  • Used Range: From C4 to G6
  • With Append: New data starts in cell C7.
  • Note: If XMAT4 has fewer columns than existing data, some columns remain unchanged.

Example 2: Sheet Test2

  • Matrices Used: XMAT5
  • Used Range: From B3 to F6
  • With Append: New data starts in cell B7.
  • Note: New data extends to column G if it has more columns than original data.

Example 3: Sheet Test3 (Header Column)

  • Matrices Used: XMAT4
  • With Append: New data starts in cell A6, including header columns.

Overwriting Data

  • Objective: Replace sheet's contents with new data.
  • Command: writeMode value overwrite sheet.

Example: Overwrite on Sheet Test1

  • Start Cell: D4
  • Result: Sheet contains only data from XMAT4, starting at D4.
  • Column Resizing: Automatically adjusted to fit new data.

Replacing an Entire Workbook

  • Objective: Replace entire workbook with new data.
  • Command: writeMode value replace file.
  • Caution: Deletes everything in the workbook, including formatting.

Example: Replace File with Sheet Test2

  • Result: w2mod.xlsx now contains only sheet Test2 with data from XMAT5.
  • Starting Cell: A1 (default if no range is specified).

Conclusion

  • Next steps involve reading, modifying, and writing back data in Excel worksheets with MATLAB.