Skip to content
EX-506 · MATLAB/Quick Revision Short Notes

MATLAB (EX-506) - Unit 3 Short Notes

UNIT 3: DATA MANAGEMENT, VISUALIZATION & ADVANCED PROGRAMMING


3.1 Advanced Data Structures & Organization

Cell Arrays

  • Definition: Arrays whose elements are containers (cells) that can hold data of different types and sizes (e.g., a string, a matrix, a struct).

  • Creation: C = {1, 'text', magic(3)}; or C = cell(2,3); (preallocate empty cells).

  • Indexing & Access:

    • C(1,2) returns a cell containing the element.

    • C{1,2} returns the contents of the cell (e.g., the string 'text').

  • When to Use: Heterogeneous data, text/mixed-type collections, or when you need to store arrays of different sizes in one container.

  • Conversion:

    • cell2mat(C) → Converts a cell array of scalars/equal-sized arrays into a standard numeric array.

    • num2cell(A) → Converts a numeric array into a cell array of the same size.

[!TIP] Common Pitfall: Using () vs {} incorrectly. () accesses the cell itself; {} accesses the data inside. Assigning with {} requires a single left-hand side.

Structures

  • Definition: Data type with named fields that can contain any data type. Ideal for organizing related, heterogeneous data.

  • Creation & Access:

    
    student.name = 'Alice';
    
    student.scores = [85 92 78];
    
    student.info.age = 20; % Nested structure
    
    
    • Access: student.name, student.scores(2).

    • Dynamic field names: field = 'age'; student.info.(field) = 21;

  • Arrays of Structures: students(1).name = 'Alice'; students(2).name = 'Bob';

  • Use Case: Storing experimental results where each subject has multiple, differently-typed attributes (ID, measurements, notes).

Tables

  • Definition: 2D container for labeled tabular data. Columns are variables (can be different types), rows are observations.

  • Creation: T = table(var1, var2, ..., 'VariableNames', {'Name1','Name2'}) or from file: T = readtable('data.csv');.

  • Data Access:

    • By row/column numbers: T{1:3, 2} (contents), T(1:3, 2) (sub-table).

    • By variable name: T.Name or T.("Name").

  • Key Operations:

    • sortrows(T, 'VariableName')

    • rowfun(@func, T, 'OutputVariableNames', {'NewVar'}) → Apply function to each row.

    • varfun(@func, T) → Apply function to each variable (column).

    • groupsummary(T, 'GroupVar') → Summary statistics by group.

  • Advantage: Self-documenting, powerful for data manipulation (grouping, joining), and integrates seamlessly with plotting functions (e.g., plot(T.Time, T.Value)).

Categorical Arrays

  • Definition: Array of categories from a finite set of discrete values (e.g., {'small','medium','large'}). Efficient for storage and grouping.

  • Creation: C = categorical({'M','F','M','F'}); or C = categorical([1 2 1 2], [1 2], {'Male','Female'});.

  • Properties:

    • categories(C) → List all possible categories.

    • ordinal vs nominal: Ordinal has a meaningful order (ordered = true), nominal does not.

    • reordercats(C, newOrder) to change order.

  • Use: Efficient grouping in groupsummary, boxplot, and controlling plot order/colors.


3.2 Data Import, Export & File I/O

Reading Data Files

Function Use Case Key Options
load('file.mat') Load MATLAB .mat files. -mat (default), variables loaded into workspace.
readtable('file.csv') Text/CSV files → Table. 'Delimiter', 'HeaderLines', 'ReadVariableNames'.
readmatrix('file.txt') Text files → Numeric matrix. 'Delimiter', 'NumHeaderLines'.
readcell('file.txt') Text files → Cell array. Similar to readmatrix, preserves mixed types.
fopen, fread, textscan Low-level I/O for complex, fixed-width, or formatted binary/text files. fopen returns file ID; textscan uses format specifiers (e.g., '%f %s').

Writing Data Files

Function Use Case Key Options
save('file.mat', 'var1', 'var2') Save variables to .mat file. '-v7.3' (large files), '-ascii' (text).
writetable(T, 'file.csv') Write Table to text/CSV. 'Delimiter', 'WriteVariableNames'.
writematrix(A, 'file.txt') Write Numeric matrix to text. 'Delimiter'.
fprintf(fileID, format, A) Low-level formatted text output. Format specifiers (%f, %s, %d), requires fopen with 'w'.

[!TIP] Common Pitfall: Forgetting to close a file with fclose(fid) after low-level I/O, which can lock the file.

Working with Specific Formats

  • Excel: Prefer readtable('file.xlsx') and writetable(T, 'file.xlsx'). Legacy xlswrite/xlsread are Windows-only and slower.

  • Binary Files: Use fwrite (write) and fread (read) with precision specifiers (e.g., 'double', 'int16').


3.3 Advanced 2D & 3D Plotting & Graphics

Fundamental Plot Customization

  • Handles: hFig = gcf; (get current figure), hAx = gca; (get current axes).

  • Properties (set via set or name-value pairs):

    • Axes: 'Title', 'XLabel', 'YLabel', 'GridLineStyle', 'XLim', 'YLim'.

    • Lines: 'LineWidth', 'LineStyle' ('-', '--', ':'), 'Marker' ('o', 's'), 'Color'.

  • Example: plot(x,y,'LineWidth',2,'Marker','o') or set(hLine, 'Color', 'r').

Specialized 2D Plots

  • bar(y) / bar(x,y) – Bar charts.

  • histogram(data) – Histogram (auto-bins).

  • area(x,y) – Filled area plot.

  • scatter(x,y) – Scatter plot (markers only).

  • stem(x,y) – Discrete data (lollipop plot).

  • errorbar(x,y,err) – Plot with error bars.

  • polarplot(theta, rho) – Polar coordinates.

3D Plotting

  • Line/Markers: plot3(x,y,z).

  • Surfaces:

    • meshgrid(x,y) → Create 2D grid for surface plots.

    • mesh(X,Y,Z) – Wireframe surface.

    • surf(X,Y,Z) – Filled surface.

    • surfc(X,Y,Z) – Surface with contour plot underneath.

    • contour(X,Y,Z) / contourf(X,Y,Z) – 2D contour lines / filled contours.

  • View Control: view(azimuth, elevation), axis equal, axis tight.

Subplots & Layout

  • Legacy: subplot(m,n,p) – Creates axes in an m×n tiling, positions p.

  • Modern (Recommended): tiledlayout(m,n) creates a tiling container. nexttile creates the next axes in the layout. Allows better spacing, shared titles/legends, and nesting.

    
    tiledlayout(2,2);
    
    nexttile; plot(x1,y1);
    
    nexttile; plot(x2,y2);
    
    

Annotations & Interactive Graphics

  • text(x,y,'string') – Place text at data coordinates.

  • annotation(type, x, y) – Place text/arrows relative to figure (0-1 normalized units). type = 'textbox', 'arrow', 'line'.

  • Plot Tools GUI: Interactive figure window for exploration (View → Plot Tools). Generates code via "Generate Code" button.

  • Interactive Plots: Create callbacks (functions) that respond to events (e.g., button click, mouse movement) by setting the figure/window's 'WindowButtonDownFcn' property.


3.4 Scripting, Functions & Code Management

Functions (Deep Dive)

  • Syntax:

    
    function [out1, out2] = functionName(in1, in2, in3)
    
        % Function body
    
        out1 = in1 + in2;
    
        out2 = in3^2;
    
    end
    
    
  • Input/Output Handling:

    • nargin → Number of actual inputs.

    • nargout → Number of actual outputs requested.

    • Use for default arguments: if nargin < 2, in2 = default; end.

  • Variable Scope:

    | Scope | Keyword | Visibility | | :--- | :--- | :--- | | Local | (none) | Only within the function. | | Global | global x | Shared across all functions & base workspace that declare it global x. | | Persistent | persistent x | Retains value between function calls (initialized only once). |

  • Function Handles:

    • Create: fh = @sin; (named function) or fh = @(x) x.^2 + 1; (anonymous).

    • Pass as argument: arrayfun(@(x) x^2, A).

    • Store in cell arrays/structures for collections of functions.

Scripts vs. Functions

Feature Script Function
Workspace Base workspace (shared). Own local workspace (isolated).
Input/Output Uses variables in base workspace. Explicit input/output arguments.
Purpose Sequence of commands, exploration. Reusable task, encapsulation.
Best Practice For running a series of commands. For any code that will be reused or needs input/output.

Code Organization

  • Scripts for Batch Runs: Main script calls functions, sets paths, generates reports.

  • Paths: addpath('folder') adds folder to search path. Use genpath for subfolders. rmpath removes.

  • Publishing: Use %% to create sections. Add markup (% <html>...</html>, % **bold**). Publish to HTML/PDF via Publish tab or publish('script.m').


3.5 Performance & Debugging

Vectorization

  • Concept: Perform operations on entire arrays at once using MATLAB's optimized, compiled matrix/array operations instead of for loops.

  • Tools:

    • Element-wise operators: .*, ./, .^.

    • Logical indexing: A(A>0) = 0;.

    • Built-in functions: sum(A, dim), mean(A), max(A,[],2).

  • Why it Matters: Vectorized code is often 10-100x faster for large arrays because loops in MATLAB are interpreted, while array operations call optimized BLAS/LAPACK libraries.

    
    % Non-vectorized (slow)
    
    for i = 1:length(A)
    
        B(i) = A(i)^2 + 5*A(i);
    
    end
    
    % Vectorized (fast)
    
    B = A.^2 + 5.*A;
    
    

Preallocation

  • Problem: Growing an array inside a loop (A = [A newElement]) forces MATLAB to repeatedly allocate new memory and copy old data → extremely slow.

  • Solution: Allocate the final size before the loop.

    
    N = 1000;
    
    A = zeros(1, N); % Preallocate with zeros, NaN, false, etc.
    
    for i = 1:N
    
        A(i) = someCalculation(i);
    
    end
    
    

Debugging Tools

  • Editor/Debugger:

    • Set/clear breakpoints (click left margin or F12).

    • Step: F10 (step over), F11 (step into), Shift+F11 (step out).

    • Examine variables in Workspace panel while paused.

  • dbstop if error → Automatically stops at the line causing an error.

  • keyboard → Inserts a breakpoint; execution pauses and gives a K>> prompt for interactive debugging.

  • Displaying Values: disp(var), fprintf('Value = %.4f\n', val) for formatted output.

Profiling

  • Purpose: Identify which functions/lines consume the most time (bottlenecks).

  • Usage:

    
    profile on -history
    
    % ... run your code ...
    
    profile viewer % Opens interactive report
    
    profile off
    
    
  • Report: Shows total time, calls, and time per call for each function. Focus optimization on functions with high "Total Time" or high "Self Time".


3.6 Introduction to Toolboxes & Applications

Concept of Toolboxes

  • Collections of specialized functions, apps, and documentation for specific domains (e.g., Signal Processing, Statistics).

  • Check Availability: ver (list all installed toolboxes), which functionName (find which toolbox a function belongs to).

Common Toolbox Spotlights

  • Curve Fitting Toolbox:

    • fit(x,y,fitType) → Fit curve to data ('poly2', 'exp1', custom fittype).

    • fitoptions → Set options (e.g., robust fitting).

  • Statistics and Machine Learning Toolbox:

    • Distributions: normpdf(x,mu,sigma), random('Normal',mu,sigma).

    • Hypothesis tests: ttest, anova1 (one-way ANOVA).

    • Clustering: kmeans.

  • Signal Processing Toolbox:

    • Filtering: y = filter(b,a,x) (IIR/FIR).

    • Frequency analysis: Y = fft(x) (Fast Fourier Transform).

App Building (Conceptual)

  • App Designer: Modern drag-and-drop environment for building apps with UI components and code in one file (.mlapp). Generates callback code automatically.

  • Programmatic UI (Legacy): Create UI components (uicontrol, uifigure) and set their properties and callbacks manually.

  • Core Components: Axes (uiaxes), buttons (Button), edit fields (EditField), drop-downs (DropDown).

  • Callbacks: Functions that execute in response to a UI event (e.g., button press). Callback functions receive two standard inputs: src (object handle that triggered event) and event (event data).

  • Sharing Data: Use app properties (in App Designer) or guidata (legacy) to store and access data across callbacks.

Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in