Initial commit for matlab

This commit is contained in:
dfty
2026-01-28 12:45:04 +08:00
commit aca5492cdd
9 changed files with 4429 additions and 0 deletions

View File

@@ -0,0 +1,479 @@
# Data Import and Export Reference
## Table of Contents
1. [Text and CSV Files](#text-and-csv-files)
2. [Spreadsheets](#spreadsheets)
3. [MAT Files](#mat-files)
4. [Images](#images)
5. [Tables and Data Types](#tables-and-data-types)
6. [Low-Level File I/O](#low-level-file-io)
## Text and CSV Files
### Reading Text Files
```matlab
% Recommended high-level functions
T = readtable('data.csv'); % Read as table (mixed types)
M = readmatrix('data.csv'); % Read as numeric matrix
C = readcell('data.csv'); % Read as cell array
S = readlines('data.txt'); % Read as string array (lines)
str = fileread('data.txt'); % Read entire file as string
% With options
T = readtable('data.csv', 'ReadVariableNames', true);
T = readtable('data.csv', 'Delimiter', ',');
T = readtable('data.csv', 'NumHeaderLines', 2);
M = readmatrix('data.csv', 'Range', 'B2:D100');
% Detect import options
opts = detectImportOptions('data.csv');
opts.VariableNames = {'Col1', 'Col2', 'Col3'};
opts.VariableTypes = {'double', 'string', 'double'};
opts.SelectedVariableNames = {'Col1', 'Col3'};
T = readtable('data.csv', opts);
```
### Writing Text Files
```matlab
% High-level functions
writetable(T, 'output.csv');
writematrix(M, 'output.csv');
writecell(C, 'output.csv');
writelines(S, 'output.txt');
% With options
writetable(T, 'output.csv', 'Delimiter', '\t');
writetable(T, 'output.csv', 'WriteVariableNames', false);
writematrix(M, 'output.csv', 'Delimiter', ',');
```
### Tab-Delimited Files
```matlab
% Reading
T = readtable('data.tsv', 'Delimiter', '\t');
T = readtable('data.txt', 'FileType', 'text', 'Delimiter', '\t');
% Writing
writetable(T, 'output.tsv', 'Delimiter', '\t');
writetable(T, 'output.txt', 'FileType', 'text', 'Delimiter', '\t');
```
## Spreadsheets
### Reading Excel Files
```matlab
% Basic reading
T = readtable('data.xlsx');
M = readmatrix('data.xlsx');
C = readcell('data.xlsx');
% Specific sheet
T = readtable('data.xlsx', 'Sheet', 'Sheet2');
T = readtable('data.xlsx', 'Sheet', 2);
% Specific range
M = readmatrix('data.xlsx', 'Range', 'B2:D100');
M = readmatrix('data.xlsx', 'Sheet', 2, 'Range', 'A1:F50');
% With options
opts = detectImportOptions('data.xlsx');
opts.Sheet = 'Data';
opts.DataRange = 'A2';
preview(opts.VariableNames) % Check column names
T = readtable('data.xlsx', opts);
% Get sheet names
[~, sheets] = xlsfinfo('data.xlsx');
```
### Writing Excel Files
```matlab
% Basic writing
writetable(T, 'output.xlsx');
writematrix(M, 'output.xlsx');
writecell(C, 'output.xlsx');
% Specific sheet and range
writetable(T, 'output.xlsx', 'Sheet', 'Results');
writetable(T, 'output.xlsx', 'Sheet', 'Data', 'Range', 'B2');
writematrix(M, 'output.xlsx', 'Sheet', 2, 'Range', 'A1');
% Append to existing sheet (use Range to specify start position)
writetable(T2, 'output.xlsx', 'Sheet', 'Data', 'WriteMode', 'append');
```
## MAT Files
### Saving Variables
```matlab
% Save all workspace variables
save('data.mat');
% Save specific variables
save('data.mat', 'x', 'y', 'results');
% Save with options
save('data.mat', 'x', 'y', '-v7.3'); % Large files (>2GB)
save('data.mat', 'x', '-append'); % Append to existing file
save('data.mat', '-struct', 's'); % Save struct fields as variables
% Compression options
save('data.mat', 'x', '-v7'); % Compressed (default)
save('data.mat', 'x', '-v6'); % Uncompressed, faster
```
### Loading Variables
```matlab
% Load all variables
load('data.mat');
% Load specific variables
load('data.mat', 'x', 'y');
% Load into structure
S = load('data.mat');
S = load('data.mat', 'x', 'y');
x = S.x;
y = S.y;
% List contents without loading
whos('-file', 'data.mat');
vars = who('-file', 'data.mat');
```
### MAT-File Object (Large Files)
```matlab
% Create MAT-file object for partial access
m = matfile('data.mat');
m.Properties.Writable = true;
% Read partial data
x = m.bigArray(1:100, :); % First 100 rows only
% Write partial data
m.bigArray(1:100, :) = newData;
% Get variable info
sz = size(m, 'bigArray');
```
## Images
### Reading Images
```matlab
% Read image
img = imread('image.png');
img = imread('image.jpg');
img = imread('image.tiff');
% Get image info
info = imfinfo('image.png');
info.Width
info.Height
info.ColorType
info.BitDepth
% Read specific frames (multi-page TIFF, GIF)
img = imread('animation.gif', 3); % Frame 3
[img, map] = imread('indexed.gif'); % Indexed image with colormap
```
### Writing Images
```matlab
% Write image
imwrite(img, 'output.png');
imwrite(img, 'output.jpg');
imwrite(img, 'output.tiff');
% With options
imwrite(img, 'output.jpg', 'Quality', 95);
imwrite(img, 'output.png', 'BitDepth', 16);
imwrite(img, 'output.tiff', 'Compression', 'lzw');
% Write indexed image with colormap
imwrite(X, map, 'indexed.gif');
% Append to multi-page TIFF
imwrite(img1, 'multipage.tiff');
imwrite(img2, 'multipage.tiff', 'WriteMode', 'append');
```
### Image Formats
```matlab
% Supported formats (partial list)
% BMP - Windows Bitmap
% GIF - Graphics Interchange Format
% JPEG - Joint Photographic Experts Group
% PNG - Portable Network Graphics
% TIFF - Tagged Image File Format
% PBM, PGM, PPM - Portable bitmap formats
% Check supported formats
formats = imformats;
```
## Tables and Data Types
### Creating Tables
```matlab
% From variables
T = table(var1, var2, var3);
T = table(var1, var2, 'VariableNames', {'Col1', 'Col2'});
% From arrays
T = array2table(M);
T = array2table(M, 'VariableNames', {'A', 'B', 'C'});
% From cell array
T = cell2table(C);
T = cell2table(C, 'VariableNames', {'Name', 'Value'});
% From struct
T = struct2table(S);
```
### Accessing Table Data
```matlab
% By variable name
col = T.VariableName;
col = T.('VariableName');
col = T{:, 'VariableName'};
% By index
row = T(5, :); % Row 5
col = T(:, 3); % Column 3 as table
data = T{:, 3}; % Column 3 as array
subset = T(1:10, 2:4); % Subset as table
data = T{1:10, 2:4}; % Subset as array
% Logical indexing
subset = T(T.Value > 5, :);
```
### Modifying Tables
```matlab
% Add variable
T.NewVar = newData;
T = addvars(T, newData, 'NewName', 'Col4');
T = addvars(T, newData, 'Before', 'ExistingCol');
% Remove variable
T.OldVar = [];
T = removevars(T, 'OldVar');
T = removevars(T, {'Col1', 'Col2'});
% Rename variable
T = renamevars(T, 'OldName', 'NewName');
T.Properties.VariableNames{'OldName'} = 'NewName';
% Reorder variables
T = movevars(T, 'Col3', 'Before', 'Col1');
T = T(:, {'Col2', 'Col1', 'Col3'});
```
### Table Operations
```matlab
% Sorting
T = sortrows(T, 'Column');
T = sortrows(T, 'Column', 'descend');
T = sortrows(T, {'Col1', 'Col2'}, {'ascend', 'descend'});
% Unique rows
T = unique(T);
T = unique(T, 'rows');
% Join tables
T = join(T1, T2); % Inner join on common keys
T = join(T1, T2, 'Keys', 'ID');
T = innerjoin(T1, T2);
T = outerjoin(T1, T2);
% Stack/unstack
T = stack(T, {'Var1', 'Var2'});
T = unstack(T, 'Values', 'Keys');
% Group operations
G = groupsummary(T, 'GroupVar', 'mean', 'ValueVar');
G = groupsummary(T, 'GroupVar', {'mean', 'std'}, 'ValueVar');
```
### Cell Arrays
```matlab
% Create cell array
C = {1, 'text', [1 2 3]};
C = cell(m, n); % Empty m×n cell array
% Access contents
contents = C{1, 2}; % Contents of cell (1,2)
subset = C(1:2, :); % Subset of cells (still cell array)
% Convert
A = cell2mat(C); % To matrix (if compatible)
T = cell2table(C); % To table
S = cell2struct(C, fields); % To struct
```
### Structures
```matlab
% Create structure
S.field1 = value1;
S.field2 = value2;
S = struct('field1', value1, 'field2', value2);
% Access fields
val = S.field1;
val = S.('field1');
% Field names
names = fieldnames(S);
tf = isfield(S, 'field1');
% Structure arrays
S(1).name = 'Alice';
S(2).name = 'Bob';
names = {S.name}; % Extract all names
```
## Low-Level File I/O
### Opening and Closing Files
```matlab
% Open file
fid = fopen('file.txt', 'r'); % Read
fid = fopen('file.txt', 'w'); % Write (overwrite)
fid = fopen('file.txt', 'a'); % Append
fid = fopen('file.bin', 'rb'); % Read binary
fid = fopen('file.bin', 'wb'); % Write binary
% Check for errors
if fid == -1
error('Could not open file');
end
% Close file
fclose(fid);
fclose('all'); % Close all files
```
### Text File I/O
```matlab
% Read formatted data
data = fscanf(fid, '%f'); % Read floats
data = fscanf(fid, '%f %f', [2 Inf]); % Two columns
C = textscan(fid, '%f %s %f'); % Mixed types
% Read lines
line = fgetl(fid); % One line (no newline)
line = fgets(fid); % One line (with newline)
% Write formatted data
fprintf(fid, '%d, %f, %s\n', intVal, floatVal, strVal);
fprintf(fid, '%6.2f\n', data);
% Read/write strings
str = fscanf(fid, '%s');
fprintf(fid, '%s', str);
```
### Binary File I/O
```matlab
% Read binary data
data = fread(fid, n, 'double'); % n doubles
data = fread(fid, [m n], 'int32'); % m×n int32s
data = fread(fid, Inf, 'uint8'); % All bytes
% Write binary data
fwrite(fid, data, 'double');
fwrite(fid, data, 'int32');
% Data types: 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
% 'int64', 'uint64', 'single', 'double', 'char'
```
### File Position
```matlab
% Get position
pos = ftell(fid);
% Set position
fseek(fid, 0, 'bof'); % Beginning of file
fseek(fid, 0, 'eof'); % End of file
fseek(fid, offset, 'cof'); % Current position + offset
% Rewind to beginning
frewind(fid);
% Check end of file
tf = feof(fid);
```
### File and Directory Operations
```matlab
% Check existence
tf = exist('file.txt', 'file');
tf = exist('folder', 'dir');
tf = isfile('file.txt');
tf = isfolder('folder');
% List files
files = dir('*.csv'); % Struct array
files = dir('folder/*.mat');
names = {files.name};
% File info
info = dir('file.txt');
info.name
info.bytes
info.date
info.datenum
% File operations
copyfile('src.txt', 'dst.txt');
movefile('src.txt', 'dst.txt');
delete('file.txt');
% Directory operations
mkdir('newfolder');
rmdir('folder');
rmdir('folder', 's'); % Remove with contents
cd('path');
pwd % Current directory
```
### Path Operations
```matlab
% Construct paths
fullpath = fullfile('folder', 'subfolder', 'file.txt');
fullpath = fullfile(pwd, 'file.txt');
% Parse paths
[path, name, ext] = fileparts('/path/to/file.txt');
% path = '/path/to', name = 'file', ext = '.txt'
% Temporary files/folders
tmpfile = tempname;
tmpdir = tempdir;
```

View File

@@ -0,0 +1,444 @@
````md
# Running MATLAB and GNU Octave Scripts from Bash
This document shows common ways to execute MATLAB-style `.m` scripts from a Bash environment using both MATLAB (MathWorks) and GNU Octave. It covers interactive use, non-interactive batch runs, passing arguments, capturing output, and practical patterns for automation and CI.
## Contents
- Requirements
- Quick comparisons
- Running MATLAB scripts from Bash
- Interactive mode
- Run a script non-interactively
- Run a function with arguments
- Run one-liners
- Working directory and path handling
- Capturing output and exit codes
- Common MATLAB flags for scripting
- Running Octave scripts from Bash
- Interactive mode
- Run a script non-interactively
- Run a function with arguments
- Run one-liners
- Making `.m` files executable (shebang)
- Working directory and path handling
- Capturing output and exit codes
- Common Octave flags for scripting
- Cross-compatibility tips (MATLAB + Octave)
- Example: a portable runner script
- Troubleshooting
## Requirements
### MATLAB
- MATLAB must be installed.
- The `matlab` executable must be on your PATH, or you must reference it by full path.
- A valid license is required to run MATLAB.
Check:
```bash
matlab -help | head
````
### GNU Octave
* Octave must be installed.
* The `octave` executable must be on your PATH.
Check:
```bash
octave --version
```
## Quick comparison
| Task | MATLAB | Octave |
| ----------------------------- | --------------------------------- | ------------------------ |
| Interactive shell | `matlab` (GUI by default) | `octave` |
| Headless run (CI) | `matlab -batch "cmd"` (preferred) | `octave --eval "cmd"` |
| Run script file | `matlab -batch "run('file.m')"` | `octave --no-gui file.m` |
| Exit with code | `exit(n)` | `exit(n)` |
| Make `.m` directly executable | uncommon | common via shebang |
## Running MATLAB scripts from Bash
### 1) Interactive mode
Starts MATLAB. Depending on your platform and install, this may launch a GUI.
```bash
matlab
```
For terminal-only use, prefer `-nodesktop` and optionally `-nosplash`:
```bash
matlab -nodesktop -nosplash
```
### 2) Run a script non-interactively
Recommended modern approach: `-batch`. It runs the command and exits when finished.
Run a script with `run()`:
```bash
matlab -batch "run('myscript.m')"
```
If the script relies on being run from its directory, set the working directory first:
```bash
matlab -batch "cd('/path/to/project'); run('myscript.m')"
```
Alternative older pattern: `-r` (less robust for automation because you must ensure MATLAB exits):
```bash
matlab -nodisplay -nosplash -r "run('myscript.m'); exit"
```
### 3) Run a function with arguments
If your file defines a function, call it directly. Prefer `-batch`:
```bash
matlab -batch "myfunc(123, 'abc')"
```
To pass values from Bash variables:
```bash
matlab -batch "myfunc(${N}, '${NAME}')"
```
If arguments may contain quotes or spaces, consider writing a small MATLAB wrapper function that reads environment variables.
### 4) Run one-liners
```bash
matlab -batch "disp(2+2)"
```
Multiple statements:
```bash
matlab -batch "a=1; b=2; fprintf('%d\n', a+b)"
```
### 5) Working directory and path handling
Common options:
* Change directory at startup:
```bash
matlab -batch "cd('/path/to/project'); myfunc()"
```
* Add code directories to MATLAB path:
```bash
matlab -batch "addpath('/path/to/lib'); myfunc()"
```
To include subfolders:
```bash
matlab -batch "addpath(genpath('/path/to/project')); myfunc()"
```
### 6) Capturing output and exit codes
Capture stdout/stderr:
```bash
matlab -batch "run('myscript.m')" > matlab.out 2>&1
```
Check exit code:
```bash
matlab -batch "run('myscript.m')"
echo $?
```
To explicitly fail a pipeline, use `exit(1)` on error. Example pattern:
```matlab
try
run('myscript.m');
catch ME
disp(getReport(ME));
exit(1);
end
exit(0);
```
Run it:
```bash
matlab -batch "try, run('myscript.m'); catch ME, disp(getReport(ME)); exit(1); end; exit(0);"
```
### 7) Common MATLAB flags for scripting
Commonly useful options:
* `-batch "cmd"`: run command, return a process exit code, then exit
* `-nodisplay`: no display (useful on headless systems)
* `-nodesktop`: no desktop GUI
* `-nosplash`: no startup splash
* `-r "cmd"`: run command; must include `exit` if you want it to terminate
Exact availability varies by MATLAB release, so use `matlab -help` for your version.
## Running GNU Octave scripts from Bash
### 1) Interactive mode
```bash
octave
```
Quieter:
```bash
octave --quiet
```
### 2) Run a script non-interactively
Run a file and exit:
```bash
octave --no-gui myscript.m
```
Quieter:
```bash
octave --quiet --no-gui myscript.m
```
Some environments use:
```bash
octave --no-window-system myscript.m
```
### 3) Run a function with arguments
If `myfunc.m` defines a function `myfunc`, call it via `--eval`:
```bash
octave --quiet --eval "myfunc(123, 'abc')"
```
If your function is not on the Octave path, add paths first:
```bash
octave --quiet --eval "addpath('/path/to/project'); myfunc()"
```
### 4) Run one-liners
```bash
octave --quiet --eval "disp(2+2)"
```
Multiple statements:
```bash
octave --quiet --eval "a=1; b=2; printf('%d\n', a+b);"
```
### 5) Making `.m` files executable (shebang)
This is a common "standalone script" pattern in Octave.
Create `myscript.m`:
```matlab
#!/usr/bin/env octave
disp("Hello from Octave");
```
Make executable:
```bash
chmod +x myscript.m
```
Run:
```bash
./myscript.m
```
If you need flags (quiet, no GUI), use a wrapper script instead, because the shebang line typically supports limited arguments across platforms.
### 6) Working directory and path handling
Change directory from the shell before running:
```bash
cd /path/to/project
octave --quiet --no-gui myscript.m
```
Or change directory within Octave:
```bash
octave --quiet --eval "cd('/path/to/project'); run('myscript.m');"
```
Add paths:
```bash
octave --quiet --eval "addpath('/path/to/lib'); run('myscript.m');"
```
### 7) Capturing output and exit codes
Capture stdout/stderr:
```bash
octave --quiet --no-gui myscript.m > octave.out 2>&1
```
Exit code:
```bash
octave --quiet --no-gui myscript.m
echo $?
```
To force non-zero exit on error, wrap execution:
```matlab
try
run('myscript.m');
catch err
disp(err.message);
exit(1);
end
exit(0);
```
Run it:
```bash
octave --quiet --eval "try, run('myscript.m'); catch err, disp(err.message); exit(1); end; exit(0);"
```
### 8) Common Octave flags for scripting
Useful options:
* `--eval "cmd"`: run a command string
* `--quiet`: suppress startup messages
* `--no-gui`: disable GUI
* `--no-window-system`: similar headless mode on some installs
* `--persist`: keep Octave open after running commands (opposite of batch behavior)
Check:
```bash
octave --help | head -n 50
```
## Cross-compatibility tips (MATLAB and Octave)
1. Prefer functions over scripts for automation
Functions give cleaner parameter passing and namespace handling.
2. Avoid toolbox-specific calls if you need portability
Many MATLAB toolboxes have no Octave equivalent.
3. Be careful with strings and quoting
MATLAB and Octave both support `'single quotes'`, and newer MATLAB supports `"double quotes"` strings. For maximum compatibility, prefer single quotes unless you know your Octave version supports double quotes the way you need.
4. Use `fprintf` or `disp` for output
For CI logs, keep output simple and deterministic.
5. Ensure exit codes reflect success or failure
In both environments, `exit(0)` indicates success, `exit(1)` indicates failure.
## Example: a portable Bash runner
This script tries MATLAB first if available, otherwise Octave.
Create `run_mfile.sh`:
```bash
#!/usr/bin/env bash
set -euo pipefail
FILE="${1:?Usage: run_mfile.sh path/to/script_or_function.m}"
CMD="${2:-}" # optional command override
if command -v matlab >/dev/null 2>&1; then
if [[ -n "$CMD" ]]; then
matlab -batch "$CMD"
else
matlab -batch "run('${FILE}')"
fi
elif command -v octave >/dev/null 2>&1; then
if [[ -n "$CMD" ]]; then
octave --quiet --no-gui --eval "$CMD"
else
octave --quiet --no-gui "$FILE"
fi
else
echo "Neither matlab nor octave found on PATH" >&2
exit 127
fi
```
Make executable:
```bash
chmod +x run_mfile.sh
```
Run:
```bash
./run_mfile.sh myscript.m
```
Or run a function call:
```bash
./run_mfile.sh myfunc.m "myfunc(1, 'abc')"
```
## Troubleshooting
### MATLAB: command not found
* Add MATLAB to PATH, or invoke it by full path, for example:
```bash
/Applications/MATLAB_R202x?.app/bin/matlab -batch "disp('ok')"
```
### Octave: GUI issues on servers
* Use `--no-gui` or `--no-window-system`.
### Scripts depend on relative paths
* `cd` into the script directory before launching, or do `cd()` within MATLAB/Octave before calling `run()`.
### Quoting problems when passing strings
* Avoid complex quoting in `--eval` or `-batch`.
* Use environment variables and read them inside MATLAB/Octave when inputs are complicated.
### Different behavior between MATLAB and Octave
* Check for unsupported functions or toolbox calls.
* Run minimal repro steps using `--eval` or `-batch` to isolate incompatibilities.

View File

@@ -0,0 +1,579 @@
# Graphics and Visualization Reference
## Table of Contents
1. [2D Plotting](#2d-plotting)
2. [3D Plotting](#3d-plotting)
3. [Specialized Plots](#specialized-plots)
4. [Figure Management](#figure-management)
5. [Customization](#customization)
6. [Exporting and Saving](#exporting-and-saving)
## 2D Plotting
### Line Plots
```matlab
% Basic line plot
plot(y); % Plot y vs index
plot(x, y); % Plot y vs x
plot(x, y, 'r-'); % Red solid line
plot(x, y, 'b--o'); % Blue dashed with circles
% Line specification: [color][marker][linestyle]
% Colors: r g b c m y k w (red, green, blue, cyan, magenta, yellow, black, white)
% Markers: o + * . x s d ^ v > < p h
% Lines: - -- : -.
% Multiple datasets
plot(x1, y1, x2, y2, x3, y3);
plot(x, [y1; y2; y3]'); % Columns as separate lines
% With properties
plot(x, y, 'LineWidth', 2, 'Color', [0.5 0.5 0.5]);
plot(x, y, 'Marker', 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
% Get handle for later modification
h = plot(x, y);
h.LineWidth = 2;
h.Color = 'red';
```
### Scatter Plots
```matlab
scatter(x, y); % Basic scatter
scatter(x, y, sz); % With marker size
scatter(x, y, sz, c); % With color
scatter(x, y, sz, c, 'filled'); % Filled markers
% sz: scalar or vector (marker sizes)
% c: color spec, scalar, vector (colormap), or RGB matrix
% Properties
scatter(x, y, 'MarkerEdgeColor', 'b', 'MarkerFaceColor', 'r');
```
### Bar Charts
```matlab
bar(y); % Vertical bars
bar(x, y); % At specified x positions
barh(y); % Horizontal bars
% Grouped and stacked
bar(Y); % Each column is a group
bar(Y, 'stacked'); % Stacked bars
% Properties
bar(y, 'FaceColor', 'b', 'EdgeColor', 'k', 'LineWidth', 1.5);
bar(y, 0.5); % Bar width (0 to 1)
```
### Area Plots
```matlab
area(y); % Filled area under curve
area(x, y);
area(Y); % Stacked areas
area(Y, 'FaceAlpha', 0.5); % Transparent
```
### Histograms
```matlab
histogram(x); % Automatic bins
histogram(x, nbins); % Number of bins
histogram(x, edges); % Specified edges
histogram(x, 'BinWidth', w); % Bin width
% Normalization
histogram(x, 'Normalization', 'probability');
histogram(x, 'Normalization', 'pdf');
histogram(x, 'Normalization', 'count'); % default
% 2D histogram
histogram2(x, y);
histogram2(x, y, 'DisplayStyle', 'tile');
histogram2(x, y, 'FaceColor', 'flat');
```
### Error Bars
```matlab
errorbar(x, y, err); % Symmetric error
errorbar(x, y, neg, pos); % Asymmetric error
errorbar(x, y, yneg, ypos, xneg, xpos); % X and Y errors
% Horizontal
errorbar(x, y, err, 'horizontal');
% With line style
errorbar(x, y, err, 'o-', 'LineWidth', 1.5);
```
### Logarithmic Plots
```matlab
semilogy(x, y); % Log y-axis
semilogx(x, y); % Log x-axis
loglog(x, y); % Both axes log
```
### Polar Plots
```matlab
polarplot(theta, rho); % Polar coordinates
polarplot(theta, rho, 'r-o'); % With line spec
% Customize polar axes
pax = polaraxes;
pax.ThetaDir = 'clockwise';
pax.ThetaZeroLocation = 'top';
```
## 3D Plotting
### Line and Scatter
```matlab
% 3D line plot
plot3(x, y, z);
plot3(x, y, z, 'r-', 'LineWidth', 2);
% 3D scatter
scatter3(x, y, z);
scatter3(x, y, z, sz, c, 'filled');
```
### Surface Plots
```matlab
% Create grid first
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X.^2 + Y.^2;
% Surface plot
surf(X, Y, Z); % Surface with edges
surf(Z); % Use indices as X, Y
% Surface properties
surf(X, Y, Z, 'FaceColor', 'interp', 'EdgeColor', 'none');
surf(X, Y, Z, 'FaceAlpha', 0.5); % Transparent
% Mesh plot (wireframe)
mesh(X, Y, Z);
mesh(X, Y, Z, 'FaceColor', 'none');
% Surface with contour below
surfc(X, Y, Z);
meshc(X, Y, Z);
```
### Contour Plots
```matlab
contour(X, Y, Z); % 2D contour
contour(X, Y, Z, n); % n contour levels
contour(X, Y, Z, levels); % Specific levels
contourf(X, Y, Z); % Filled contours
[C, h] = contour(X, Y, Z);
clabel(C, h); % Add labels
% 3D contour
contour3(X, Y, Z);
```
### Other 3D Plots
```matlab
% Bar3
bar3(Z); % 3D bar chart
bar3(Z, 'stacked');
% Pie3
pie3(X); % 3D pie chart
% Waterfall
waterfall(X, Y, Z); % Like mesh with no back lines
% Ribbon
ribbon(Y); % 3D ribbon
% Stem3
stem3(x, y, z); % 3D stem plot
```
### View and Lighting
```matlab
% Set view angle
view(az, el); % Azimuth, elevation
view(2); % Top-down (2D view)
view(3); % Default 3D view
view([1 1 1]); % View from direction
% Lighting
light; % Add light source
light('Position', [1 0 1]);
lighting gouraud; % Smooth lighting
lighting flat; % Flat shading
lighting none; % No lighting
% Material properties
material shiny;
material dull;
material metal;
% Shading
shading flat; % One color per face
shading interp; % Interpolated colors
shading faceted; % With edges (default)
```
## Specialized Plots
### Statistical Plots
```matlab
% Box plot
boxplot(data);
boxplot(data, groups); % Grouped
boxplot(data, 'Notch', 'on'); % With notches
% Violin plot (R2023b+)
violinplot(data);
% Heatmap
heatmap(data);
heatmap(xLabels, yLabels, data);
heatmap(T, 'XVariable', 'Col1', 'YVariable', 'Col2', 'ColorVariable', 'Val');
% Parallel coordinates
parallelplot(data);
```
### Image Display
```matlab
% Display image
imshow(img); % Auto-scaled
imshow(img, []); % Scale to full range
imshow(img, [low high]); % Specify display range
% Image as plot
image(C); % Direct indexed colors
imagesc(data); % Scaled colors
imagesc(data, [cmin cmax]); % Specify color limits
% Colormap for imagesc
imagesc(data);
colorbar;
colormap(jet);
```
### Quiver and Stream
```matlab
% Vector field
[X, Y] = meshgrid(-2:0.5:2);
U = -Y;
V = X;
quiver(X, Y, U, V); % 2D arrows
quiver3(X, Y, Z, U, V, W); % 3D arrows
% Streamlines
streamline(X, Y, U, V, startx, starty);
```
### Pie and Donut
```matlab
pie(X); % Pie chart
pie(X, explode); % Explode slices (logical)
pie(X, labels); % With labels
% Donut (using patch or workaround)
pie(X);
% Add white circle in center for donut effect
```
## Figure Management
### Creating Figures
```matlab
figure; % New figure window
figure(n); % Figure with number n
fig = figure; % Get handle
fig = figure('Name', 'My Figure', 'Position', [100 100 800 600]);
% Figure properties
fig.Color = 'white';
fig.Units = 'pixels';
fig.Position = [left bottom width height];
```
### Subplots
```matlab
subplot(m, n, p); % m×n grid, position p
subplot(2, 2, 1); % Top-left of 2×2
% Spanning multiple positions
subplot(2, 2, [1 2]); % Top row
% With gap control
tiledlayout(2, 2); % Modern alternative
nexttile;
plot(x1, y1);
nexttile;
plot(x2, y2);
% Tile spanning
nexttile([1 2]); % Span 2 columns
```
### Hold and Overlay
```matlab
hold on; % Keep existing, add new plots
plot(x1, y1);
plot(x2, y2);
hold off; % Release
% Alternative
hold(ax, 'on');
hold(ax, 'off');
```
### Multiple Axes
```matlab
% Two y-axes
yyaxis left;
plot(x, y1);
ylabel('Left Y');
yyaxis right;
plot(x, y2);
ylabel('Right Y');
% Linked axes
ax1 = subplot(2,1,1); plot(x, y1);
ax2 = subplot(2,1,2); plot(x, y2);
linkaxes([ax1, ax2], 'x'); % Link x-axes
```
### Current Objects
```matlab
gcf; % Current figure handle
gca; % Current axes handle
gco; % Current object handle
% Set current
figure(fig);
axes(ax);
```
## Customization
### Labels and Title
```matlab
title('My Title');
title('My Title', 'FontSize', 14, 'FontWeight', 'bold');
xlabel('X Label');
ylabel('Y Label');
zlabel('Z Label'); % For 3D
% With interpreter
title('$$\int_0^1 x^2 dx$$', 'Interpreter', 'latex');
xlabel('Time (s)', 'Interpreter', 'none');
```
### Legend
```matlab
legend('Series 1', 'Series 2');
legend({'Series 1', 'Series 2'});
legend('Location', 'best'); % Auto-place
legend('Location', 'northeast');
legend('Location', 'northeastoutside');
% With specific plots
h1 = plot(x1, y1);
h2 = plot(x2, y2);
legend([h1, h2], {'Data 1', 'Data 2'});
legend('off'); % Remove legend
legend('boxoff'); % Remove box
```
### Axis Control
```matlab
axis([xmin xmax ymin ymax]); % Set limits
axis([xmin xmax ymin ymax zmin zmax]); % 3D
xlim([xmin xmax]);
ylim([ymin ymax]);
zlim([zmin zmax]);
axis equal; % Equal aspect ratio
axis square; % Square axes
axis tight; % Fit to data
axis auto; % Automatic
axis off; % Hide axes
axis on; % Show axes
% Reverse direction
set(gca, 'YDir', 'reverse');
set(gca, 'XDir', 'reverse');
```
### Grid and Box
```matlab
grid on;
grid off;
grid minor; % Minor grid lines
box on; % Show box
box off; % Hide box
```
### Ticks
```matlab
xticks([0 1 2 3 4 5]);
yticks(0:0.5:3);
xticklabels({'A', 'B', 'C', 'D', 'E', 'F'});
yticklabels({'Low', 'Medium', 'High'});
xtickangle(45); % Rotate labels
ytickformat('%.2f'); % Format
xtickformat('usd'); % Currency
```
### Colors and Colormaps
```matlab
% Predefined colormaps
colormap(jet);
colormap(parula); % Default
colormap(hot);
colormap(cool);
colormap(gray);
colormap(bone);
colormap(hsv);
colormap(turbo);
colormap(viridis);
% Colorbar
colorbar;
colorbar('Location', 'eastoutside');
caxis([cmin cmax]); % Color limits
clim([cmin cmax]); % R2022a+ syntax
% Custom colormap
cmap = [1 0 0; 0 1 0; 0 0 1]; % Red, green, blue
colormap(cmap);
% Color order for lines
colororder(colors); % R2019b+
```
### Text and Annotations
```matlab
% Add text
text(x, y, 'Label');
text(x, y, z, 'Label'); % 3D
text(x, y, 'Label', 'FontSize', 12, 'Color', 'red');
text(x, y, 'Label', 'HorizontalAlignment', 'center');
% Annotations
annotation('arrow', [x1 x2], [y1 y2]);
annotation('textarrow', [x1 x2], [y1 y2], 'String', 'Peak');
annotation('ellipse', [x y w h]);
annotation('rectangle', [x y w h]);
annotation('line', [x1 x2], [y1 y2]);
% Text with LaTeX
text(x, y, '$$\alpha = \beta^2$$', 'Interpreter', 'latex');
```
### Lines and Shapes
```matlab
% Reference lines
xline(5); % Vertical line at x=5
yline(10); % Horizontal line at y=10
xline(5, '--r', 'Threshold'); % With label
% Shapes
rectangle('Position', [x y w h]);
rectangle('Position', [x y w h], 'Curvature', [0.2 0.2]); % Rounded
% Patches (filled polygons)
patch(xv, yv, 'blue');
patch(xv, yv, zv, 'blue'); % 3D
```
## Exporting and Saving
### Save Figure
```matlab
saveas(gcf, 'figure.png');
saveas(gcf, 'figure.fig'); % MATLAB figure file
saveas(gcf, 'figure.pdf');
saveas(gcf, 'figure.eps');
```
### Print Command
```matlab
print('-dpng', 'figure.png');
print('-dpng', '-r300', 'figure.png'); % 300 DPI
print('-dpdf', 'figure.pdf');
print('-dsvg', 'figure.svg');
print('-deps', 'figure.eps');
print('-depsc', 'figure.eps'); % Color EPS
% Vector formats for publication
print('-dpdf', '-painters', 'figure.pdf');
print('-dsvg', '-painters', 'figure.svg');
```
### Export Graphics (R2020a+)
```matlab
exportgraphics(gcf, 'figure.png');
exportgraphics(gcf, 'figure.png', 'Resolution', 300);
exportgraphics(gcf, 'figure.pdf', 'ContentType', 'vector');
exportgraphics(gca, 'axes_only.png'); % Just the axes
% For presentations/documents
exportgraphics(gcf, 'figure.emf'); % Windows
exportgraphics(gcf, 'figure.eps'); % LaTeX
```
### Copy to Clipboard
```matlab
copygraphics(gcf); % Copy current figure
copygraphics(gca); % Copy current axes
copygraphics(gcf, 'ContentType', 'vector');
```
### Paper Size (for Printing)
```matlab
set(gcf, 'PaperUnits', 'inches');
set(gcf, 'PaperPosition', [0 0 6 4]);
set(gcf, 'PaperSize', [6 4]);
set(gcf, 'PaperPositionMode', 'auto');
```

553
references/mathematics.md Normal file
View File

@@ -0,0 +1,553 @@
# Mathematics Reference
## Table of Contents
1. [Linear Algebra](#linear-algebra)
2. [Elementary Math](#elementary-math)
3. [Calculus and Integration](#calculus-and-integration)
4. [Differential Equations](#differential-equations)
5. [Optimization](#optimization)
6. [Statistics](#statistics)
7. [Signal Processing](#signal-processing)
8. [Interpolation and Fitting](#interpolation-and-fitting)
## Linear Algebra
### Solving Linear Systems
```matlab
% Ax = b
x = A \ b; % Preferred method (mldivide)
x = linsolve(A, b); % With options
x = inv(A) * b; % Less efficient, avoid
% Options for linsolve
opts.LT = true; % Lower triangular
opts.UT = true; % Upper triangular
opts.SYM = true; % Symmetric
opts.POSDEF = true; % Positive definite
x = linsolve(A, b, opts);
% xA = b
x = b / A; % mrdivide
% Least squares (overdetermined system)
x = A \ b; % Minimum norm solution
x = lsqminnorm(A, b); % Explicit minimum norm
% Nonnegative least squares
x = lsqnonneg(A, b); % x >= 0 constraint
```
### Matrix Decompositions
```matlab
% LU decomposition: A = L*U or P*A = L*U
[L, U] = lu(A); % L may not be lower triangular
[L, U, P] = lu(A); % P*A = L*U
% QR decomposition: A = Q*R
[Q, R] = qr(A); % Full decomposition
[Q, R] = qr(A, 0); % Economy size
[Q, R, P] = qr(A); % Column pivoting: A*P = Q*R
% Cholesky: A = R'*R (symmetric positive definite)
R = chol(A); % Upper triangular
L = chol(A, 'lower'); % Lower triangular
% LDL': A = L*D*L' (symmetric)
[L, D] = ldl(A);
% Schur decomposition: A = U*T*U'
[U, T] = schur(A); % T is quasi-triangular
[U, T] = schur(A, 'complex'); % T is triangular
```
### Eigenvalues and Eigenvectors
```matlab
% Eigenvalues
e = eig(A); % Eigenvalues only
[V, D] = eig(A); % V: eigenvectors, D: diagonal eigenvalues
% A*V = V*D
% Generalized eigenvalues: A*v = lambda*B*v
e = eig(A, B);
[V, D] = eig(A, B);
% Sparse/large matrices (subset of eigenvalues)
e = eigs(A, k); % k largest magnitude
e = eigs(A, k, 'smallestabs'); % k smallest magnitude
[V, D] = eigs(A, k, 'largestreal');
```
### Singular Value Decomposition
```matlab
% SVD: A = U*S*V'
[U, S, V] = svd(A); % Full decomposition
[U, S, V] = svd(A, 'econ'); % Economy size
s = svd(A); % Singular values only
% Sparse/large matrices
[U, S, V] = svds(A, k); % k largest singular values
% Applications
r = rank(A); % Rank (count nonzero singular values)
p = pinv(A); % Pseudoinverse (via SVD)
n = norm(A, 2); % 2-norm = largest singular value
c = cond(A); % Condition number = ratio of largest/smallest
```
### Matrix Properties
```matlab
d = det(A); % Determinant
t = trace(A); % Trace (sum of diagonal)
r = rank(A); % Rank
n = norm(A); % 2-norm (default)
n = norm(A, 1); % 1-norm (max column sum)
n = norm(A, inf); % Inf-norm (max row sum)
n = norm(A, 'fro'); % Frobenius norm
c = cond(A); % Condition number
c = rcond(A); % Reciprocal condition (fast estimate)
```
## Elementary Math
### Trigonometric Functions
```matlab
% Radians
y = sin(x); y = cos(x); y = tan(x);
y = asin(x); y = acos(x); y = atan(x);
y = atan2(y, x); % Four-quadrant arctangent
% Degrees
y = sind(x); y = cosd(x); y = tand(x);
y = asind(x); y = acosd(x); y = atand(x);
% Hyperbolic
y = sinh(x); y = cosh(x); y = tanh(x);
y = asinh(x); y = acosh(x); y = atanh(x);
% Secant, cosecant, cotangent
y = sec(x); y = csc(x); y = cot(x);
```
### Exponentials and Logarithms
```matlab
y = exp(x); % e^x
y = log(x); % Natural log (ln)
y = log10(x); % Log base 10
y = log2(x); % Log base 2
y = log1p(x); % log(1+x), accurate for small x
[F, E] = log2(x); % F * 2^E = x
y = sqrt(x); % Square root
y = nthroot(x, n); % Real n-th root
y = realsqrt(x); % Real square root (error if x < 0)
y = pow2(x); % 2^x
y = x .^ y; % Element-wise power
```
### Complex Numbers
```matlab
z = complex(a, b); % a + bi
z = 3 + 4i; % Direct creation
r = real(z); % Real part
i = imag(z); % Imaginary part
m = abs(z); % Magnitude
p = angle(z); % Phase angle (radians)
c = conj(z); % Complex conjugate
[theta, rho] = cart2pol(x, y); % Cartesian to polar
[x, y] = pol2cart(theta, rho); % Polar to Cartesian
```
### Rounding and Remainders
```matlab
y = round(x); % Round to nearest integer
y = round(x, n); % Round to n decimal places
y = floor(x); % Round toward -infinity
y = ceil(x); % Round toward +infinity
y = fix(x); % Round toward zero
y = mod(x, m); % Modulo (sign of m)
y = rem(x, m); % Remainder (sign of x)
[q, r] = deconv(x, m); % Quotient and remainder
y = sign(x); % Sign (-1, 0, or 1)
y = abs(x); % Absolute value
```
### Special Functions
```matlab
y = gamma(x); % Gamma function
y = gammaln(x); % Log gamma (avoid overflow)
y = factorial(n); % n!
y = nchoosek(n, k); % Binomial coefficient
y = erf(x); % Error function
y = erfc(x); % Complementary error function
y = erfcinv(x); % Inverse complementary error function
y = besselj(nu, x); % Bessel J
y = bessely(nu, x); % Bessel Y
y = besseli(nu, x); % Modified Bessel I
y = besselk(nu, x); % Modified Bessel K
y = legendre(n, x); % Legendre polynomials
```
## Calculus and Integration
### Numerical Integration
```matlab
% Definite integrals
q = integral(fun, a, b); % Integrate fun from a to b
q = integral(@(x) x.^2, 0, 1); % Example: integral of x^2
% Options
q = integral(fun, a, b, 'AbsTol', 1e-10);
q = integral(fun, a, b, 'RelTol', 1e-6);
% Improper integrals
q = integral(fun, 0, Inf); % Integrate to infinity
q = integral(fun, -Inf, Inf); % Full real line
% Multidimensional
q = integral2(fun, xa, xb, ya, yb); % Double integral
q = integral3(fun, xa, xb, ya, yb, za, zb); % Triple integral
% From discrete data
q = trapz(x, y); % Trapezoidal rule
q = trapz(y); % Unit spacing
q = cumtrapz(x, y); % Cumulative integral
```
### Numerical Differentiation
```matlab
% Finite differences
dy = diff(y); % First differences
dy = diff(y, n); % n-th differences
dy = diff(y, n, dim); % Along dimension
% Gradient (numerical derivative)
g = gradient(y); % dy/dx, unit spacing
g = gradient(y, h); % dy/dx, spacing h
[gx, gy] = gradient(Z, hx, hy); % Gradient of 2D data
```
## Differential Equations
### ODE Solvers
```matlab
% Standard form: dy/dt = f(t, y)
odefun = @(t, y) -2*y; % Example: dy/dt = -2y
[t, y] = ode45(odefun, tspan, y0);
% Solver selection:
% ode45 - Nonstiff, medium accuracy (default choice)
% ode23 - Nonstiff, low accuracy
% ode113 - Nonstiff, variable order
% ode15s - Stiff, variable order (try if ode45 is slow)
% ode23s - Stiff, low order
% ode23t - Moderately stiff, trapezoidal
% ode23tb - Stiff, TR-BDF2
% With options
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-9);
options = odeset('MaxStep', 0.1);
options = odeset('Events', @myEventFcn); % Stop conditions
[t, y] = ode45(odefun, tspan, y0, options);
```
### Higher-Order ODEs
```matlab
% y'' + 2y' + y = 0, y(0) = 1, y'(0) = 0
% Convert to system: y1 = y, y2 = y'
% y1' = y2
% y2' = -2*y2 - y1
odefun = @(t, y) [y(2); -2*y(2) - y(1)];
y0 = [1; 0]; % [y(0); y'(0)]
[t, y] = ode45(odefun, [0 10], y0);
plot(t, y(:,1)); % Plot y (first component)
```
### Boundary Value Problems
```matlab
% y'' + |y| = 0, y(0) = 0, y(4) = -2
solinit = bvpinit(linspace(0, 4, 5), [0; 0]);
sol = bvp4c(@odefun, @bcfun, solinit);
function dydx = odefun(x, y)
dydx = [y(2); -abs(y(1))];
end
function res = bcfun(ya, yb)
res = [ya(1); yb(1) + 2]; % y(0) = 0, y(4) = -2
end
```
## Optimization
### Unconstrained Optimization
```matlab
% Single variable, bounded
[x, fval] = fminbnd(fun, x1, x2);
[x, fval] = fminbnd(@(x) x.^2 - 4*x, 0, 5);
% Multivariable, unconstrained
[x, fval] = fminsearch(fun, x0);
options = optimset('TolX', 1e-8, 'TolFun', 1e-8);
[x, fval] = fminsearch(fun, x0, options);
% Display iterations
options = optimset('Display', 'iter');
```
### Root Finding
```matlab
% Find where f(x) = 0
x = fzero(fun, x0); % Near x0
x = fzero(fun, [x1 x2]); % In interval [x1, x2]
x = fzero(@(x) cos(x) - x, 0.5);
% Polynomial roots
r = roots([1 0 -4]); % Roots of x^2 - 4 = 0
% Returns [2; -2]
```
### Least Squares
```matlab
% Linear least squares: minimize ||Ax - b||
x = A \ b; % Standard solution
x = lsqminnorm(A, b); % Minimum norm solution
% Nonnegative least squares
x = lsqnonneg(A, b); % x >= 0
% Nonlinear least squares
x = lsqnonlin(fun, x0); % Minimize sum(fun(x).^2)
x = lsqcurvefit(fun, x0, xdata, ydata); % Curve fitting
```
## Statistics
### Descriptive Statistics
```matlab
% Central tendency
m = mean(x); % Arithmetic mean
m = mean(x, 'all'); % Mean of all elements
m = mean(x, dim); % Mean along dimension
m = mean(x, 'omitnan'); % Ignore NaN values
gm = geomean(x); % Geometric mean
hm = harmmean(x); % Harmonic mean
med = median(x); % Median
mo = mode(x); % Mode
% Dispersion
s = std(x); % Standard deviation (N-1)
s = std(x, 1); % Population std (N)
v = var(x); % Variance
r = range(x); % max - min
iqr_val = iqr(x); % Interquartile range
% Extremes
[minv, mini] = min(x);
[maxv, maxi] = max(x);
[lo, hi] = bounds(x); % Min and max together
```
### Correlation and Covariance
```matlab
% Correlation
R = corrcoef(X, Y); % Correlation matrix
r = corrcoef(x, y); % Correlation coefficient
% Covariance
C = cov(X, Y); % Covariance matrix
c = cov(x, y); % Covariance
% Cross-correlation (signal processing)
[r, lags] = xcorr(x, y); % Cross-correlation
[r, lags] = xcorr(x, y, 'coeff'); % Normalized
```
### Percentiles and Quantiles
```matlab
p = prctile(x, [25 50 75]); % Percentiles
q = quantile(x, [0.25 0.5 0.75]); % Quantiles
```
### Moving Statistics
```matlab
y = movmean(x, k); % k-point moving average
y = movmedian(x, k); % Moving median
y = movstd(x, k); % Moving standard deviation
y = movvar(x, k); % Moving variance
y = movmin(x, k); % Moving minimum
y = movmax(x, k); % Moving maximum
y = movsum(x, k); % Moving sum
% Window options
y = movmean(x, [kb kf]); % kb back, kf forward
y = movmean(x, k, 'omitnan'); % Ignore NaN
```
### Histograms and Distributions
```matlab
% Histogram counts
[N, edges] = histcounts(x); % Automatic binning
[N, edges] = histcounts(x, nbins); % Specify number of bins
[N, edges] = histcounts(x, edges); % Specify edges
% Probability/normalized
[N, edges] = histcounts(x, 'Normalization', 'probability');
[N, edges] = histcounts(x, 'Normalization', 'pdf');
% 2D histogram
[N, xedges, yedges] = histcounts2(x, y);
```
## Signal Processing
### Fourier Transform
```matlab
% FFT
Y = fft(x); % 1D FFT
Y = fft(x, n); % n-point FFT (zero-pad/truncate)
Y = fft2(X); % 2D FFT
Y = fftn(X); % N-D FFT
% Inverse FFT
x = ifft(Y);
X = ifft2(Y);
X = ifftn(Y);
% Shift zero-frequency to center
Y_shifted = fftshift(Y);
Y = ifftshift(Y_shifted);
% Frequency axis
n = length(x);
fs = 1000; % Sampling frequency
f = (0:n-1) * fs / n; % Frequency vector
f = (-n/2:n/2-1) * fs / n; % Centered frequency vector
```
### Filtering
```matlab
% 1D filtering
y = filter(b, a, x); % Apply IIR/FIR filter
y = filtfilt(b, a, x); % Zero-phase filtering
% Simple moving average
b = ones(1, k) / k;
y = filter(b, 1, x);
% Convolution
y = conv(x, h); % Full convolution
y = conv(x, h, 'same'); % Same size as x
y = conv(x, h, 'valid'); % Valid part only
% Deconvolution
[q, r] = deconv(y, h); % y = conv(q, h) + r
% 2D filtering
Y = filter2(H, X); % 2D filter
Y = conv2(X, H, 'same'); % 2D convolution
```
## Interpolation and Fitting
### Interpolation
```matlab
% 1D interpolation
yi = interp1(x, y, xi); % Linear (default)
yi = interp1(x, y, xi, 'spline'); % Spline
yi = interp1(x, y, xi, 'pchip'); % Piecewise cubic
yi = interp1(x, y, xi, 'nearest'); % Nearest neighbor
% 2D interpolation
zi = interp2(X, Y, Z, xi, yi);
zi = interp2(X, Y, Z, xi, yi, 'spline');
% 3D interpolation
vi = interp3(X, Y, Z, V, xi, yi, zi);
% Scattered data
F = scatteredInterpolant(x, y, v);
vi = F(xi, yi);
```
### Polynomial Fitting
```matlab
% Polynomial fit
p = polyfit(x, y, n); % Fit degree-n polynomial
% p = [p1, p2, ..., pn+1]
% y = p1*x^n + p2*x^(n-1) + ... + pn+1
% Evaluate polynomial
yi = polyval(p, xi);
% With fit quality
[p, S] = polyfit(x, y, n);
[yi, delta] = polyval(p, xi, S); % delta = error estimate
% Polynomial operations
r = roots(p); % Find roots
p = poly(r); % Polynomial from roots
q = polyder(p); % Derivative
q = polyint(p); % Integral
c = conv(p1, p2); % Multiply polynomials
[q, r] = deconv(p1, p2); % Divide polynomials
```
### Curve Fitting
```matlab
% Using fit function (Curve Fitting Toolbox or basic forms)
% Linear: y = a*x + b
p = polyfit(x, y, 1);
a = p(1); b = p(2);
% Exponential: y = a*exp(b*x)
% Linearize: log(y) = log(a) + b*x
p = polyfit(x, log(y), 1);
b = p(1); a = exp(p(2));
% Power: y = a*x^b
% Linearize: log(y) = log(a) + b*log(x)
p = polyfit(log(x), log(y), 1);
b = p(1); a = exp(p(2));
% General nonlinear fitting with lsqcurvefit
model = @(p, x) p(1)*exp(-p(2)*x); % Example: a*exp(-b*x)
p0 = [1, 1]; % Initial guess
p = lsqcurvefit(model, p0, xdata, ydata);
```

View File

@@ -0,0 +1,349 @@
# Matrices and Arrays Reference
## Table of Contents
1. [Array Creation](#array-creation)
2. [Indexing and Subscripting](#indexing-and-subscripting)
3. [Array Manipulation](#array-manipulation)
4. [Concatenation and Reshaping](#concatenation-and-reshaping)
5. [Array Information](#array-information)
6. [Sorting and Searching](#sorting-and-searching)
## Array Creation
### Basic Creation
```matlab
% Direct specification
A = [1 2 3; 4 5 6; 7 8 9]; % 3x3 matrix (rows separated by ;)
v = [1, 2, 3, 4, 5]; % Row vector
v = [1; 2; 3; 4; 5]; % Column vector
% Range operators
v = 1:10; % 1 to 10, step 1
v = 0:0.5:5; % 0 to 5, step 0.5
v = 10:-1:1; % 10 down to 1
% Linearly/logarithmically spaced
v = linspace(0, 1, 100); % 100 points from 0 to 1
v = logspace(0, 3, 50); % 50 points from 10^0 to 10^3
```
### Special Matrices
```matlab
% Common patterns
I = eye(n); % n×n identity matrix
I = eye(m, n); % m×n identity matrix
Z = zeros(m, n); % m×n zeros
O = ones(m, n); % m×n ones
D = diag([1 2 3]); % Diagonal matrix from vector
d = diag(A); % Extract diagonal from matrix
% Random matrices
R = rand(m, n); % Uniform [0,1]
R = randn(m, n); % Normal (mean=0, std=1)
R = randi([a b], m, n); % Random integers in [a,b]
R = randperm(n); % Random permutation of 1:n
% Logical arrays
T = true(m, n); % All true
F = false(m, n); % All false
% Grids for 2D/3D
[X, Y] = meshgrid(x, y); % 2D grid from vectors
[X, Y, Z] = meshgrid(x, y, z); % 3D grid
[X, Y] = ndgrid(x, y); % Alternative (different orientation)
```
### Creating from Existing
```matlab
A_like = zeros(size(B)); % Same size as B
A_like = ones(size(B), 'like', B); % Same size and type as B
A_copy = A; % Copy (by value, not reference)
```
## Indexing and Subscripting
### Basic Indexing
```matlab
% Single element (1-based indexing)
elem = A(2, 3); % Row 2, column 3
elem = A(5); % Linear index (column-major order)
% Ranges
row = A(2, :); % Entire row 2
col = A(:, 3); % Entire column 3
sub = A(1:2, 2:3); % Rows 1-2, columns 2-3
% End keyword
last = A(end, :); % Last row
last3 = A(end-2:end, :); % Last 3 rows
```
### Logical Indexing
```matlab
% Find elements meeting condition
idx = A > 5; % Logical array
elements = A(A > 5); % Extract elements > 5
A(A < 0) = 0; % Set negative elements to 0
% Combine conditions
idx = (A > 0) & (A < 10); % AND
idx = (A < 0) | (A > 10); % OR
idx = ~(A == 0); % NOT
```
### Linear Indexing
```matlab
% Convert between linear and subscript indices
[row, col] = ind2sub(size(A), linearIdx);
linearIdx = sub2ind(size(A), row, col);
% Find indices of nonzero/condition
idx = find(A > 5); % Linear indices where A > 5
idx = find(A > 5, k); % First k indices
idx = find(A > 5, k, 'last'); % Last k indices
[row, col] = find(A > 5); % Subscript indices
```
### Advanced Indexing
```matlab
% Index with arrays
rows = [1 3 5];
cols = [2 4];
sub = A(rows, cols); % Submatrix
% Logical indexing with another array
B = A(logical_mask); % Elements where mask is true
% Assignment with indexing
A(1:2, 1:2) = [10 20; 30 40]; % Assign submatrix
A(:) = 1:numel(A); % Assign all elements (column-major)
```
## Array Manipulation
### Element-wise Operations
```matlab
% Arithmetic (element-wise uses . prefix)
C = A + B; % Addition
C = A - B; % Subtraction
C = A .* B; % Element-wise multiplication
C = A ./ B; % Element-wise division
C = A .\ B; % Element-wise left division (B./A)
C = A .^ n; % Element-wise power
% Comparison (element-wise)
C = A == B; % Equal
C = A ~= B; % Not equal
C = A < B; % Less than
C = A <= B; % Less than or equal
C = A > B; % Greater than
C = A >= B; % Greater than or equal
```
### Matrix Operations
```matlab
% Matrix arithmetic
C = A * B; % Matrix multiplication
C = A ^ n; % Matrix power
C = A'; % Conjugate transpose
C = A.'; % Transpose (no conjugate)
% Matrix functions
B = inv(A); % Inverse
B = pinv(A); % Pseudoinverse
d = det(A); % Determinant
t = trace(A); % Trace (sum of diagonal)
r = rank(A); % Rank
n = norm(A); % Matrix/vector norm
n = norm(A, 'fro'); % Frobenius norm
% Solve linear systems
x = A \ b; % Solve Ax = b
x = b' / A'; % Solve xA = b
```
### Common Functions
```matlab
% Apply to each element
B = abs(A); % Absolute value
B = sqrt(A); % Square root
B = exp(A); % Exponential
B = log(A); % Natural log
B = log10(A); % Log base 10
B = sin(A); % Sine (radians)
B = sind(A); % Sine (degrees)
B = round(A); % Round to nearest integer
B = floor(A); % Round down
B = ceil(A); % Round up
B = real(A); % Real part
B = imag(A); % Imaginary part
B = conj(A); % Complex conjugate
```
## Concatenation and Reshaping
### Concatenation
```matlab
% Horizontal (side by side)
C = [A B]; % Concatenate columns
C = [A, B]; % Same as above
C = horzcat(A, B); % Function form
C = cat(2, A, B); % Concatenate along dimension 2
% Vertical (stacked)
C = [A; B]; % Concatenate rows
C = vertcat(A, B); % Function form
C = cat(1, A, B); % Concatenate along dimension 1
% Block diagonal
C = blkdiag(A, B, C); % Block diagonal matrix
```
### Reshaping
```matlab
% Reshape
B = reshape(A, m, n); % Reshape to m×n (same total elements)
B = reshape(A, [], n); % Auto-compute rows
v = A(:); % Flatten to column vector
% Transpose and permute
B = A'; % Transpose 2D
B = permute(A, [2 1 3]); % Permute dimensions
B = ipermute(A, [2 1 3]); % Inverse permute
% Remove/add dimensions
B = squeeze(A); % Remove singleton dimensions
B = shiftdim(A, n); % Shift dimensions
% Replication
B = repmat(A, m, n); % Tile m×n times
B = repelem(A, m, n); % Repeat elements
```
### Flipping and Rotating
```matlab
B = flip(A); % Flip along first non-singleton dimension
B = flip(A, dim); % Flip along dimension dim
B = fliplr(A); % Flip left-right (columns)
B = flipud(A); % Flip up-down (rows)
B = rot90(A); % Rotate 90° counterclockwise
B = rot90(A, k); % Rotate k×90°
B = circshift(A, k); % Circular shift
```
## Array Information
### Size and Dimensions
```matlab
[m, n] = size(A); % Rows and columns
m = size(A, 1); % Number of rows
n = size(A, 2); % Number of columns
sz = size(A); % Size vector
len = length(A); % Largest dimension
num = numel(A); % Total number of elements
ndim = ndims(A); % Number of dimensions
```
### Type Checking
```matlab
tf = isempty(A); % Is empty?
tf = isscalar(A); % Is scalar (1×1)?
tf = isvector(A); % Is vector (1×n or n×1)?
tf = isrow(A); % Is row vector?
tf = iscolumn(A); % Is column vector?
tf = ismatrix(A); % Is 2D matrix?
tf = isnumeric(A); % Is numeric?
tf = isreal(A); % Is real (no imaginary)?
tf = islogical(A); % Is logical?
tf = isnan(A); % Which elements are NaN?
tf = isinf(A); % Which elements are Inf?
tf = isfinite(A); % Which elements are finite?
```
### Comparison
```matlab
tf = isequal(A, B); % Are arrays equal?
tf = isequaln(A, B); % Equal, treating NaN as equal?
tf = all(A); % All nonzero/true?
tf = any(A); % Any nonzero/true?
tf = all(A, dim); % All along dimension
tf = any(A, dim); % Any along dimension
```
## Sorting and Searching
### Sorting
```matlab
B = sort(A); % Sort columns ascending
B = sort(A, 'descend'); % Sort descending
B = sort(A, dim); % Sort along dimension
[B, idx] = sort(A); % Also return original indices
B = sortrows(A); % Sort rows by first column
B = sortrows(A, col); % Sort by specific column(s)
B = sortrows(A, col, 'descend');
```
### Unique and Set Operations
```matlab
B = unique(A); % Unique elements
[B, ia, ic] = unique(A); % With index information
B = unique(A, 'rows'); % Unique rows
% Set operations
C = union(A, B); % Union
C = intersect(A, B); % Intersection
C = setdiff(A, B); % A - B (in A but not B)
C = setxor(A, B); % Symmetric difference
tf = ismember(A, B); % Is each element of A in B?
```
### Min/Max
```matlab
m = min(A); % Column minimums
m = min(A, [], 'all'); % Global minimum
[m, idx] = min(A); % With indices
m = min(A, B); % Element-wise minimum
M = max(A); % Column maximums
M = max(A, [], 'all'); % Global maximum
[M, idx] = max(A); % With indices
[minVal, minIdx] = min(A(:)); % Global min with linear index
[maxVal, maxIdx] = max(A(:)); % Global max with linear index
% k smallest/largest
B = mink(A, k); % k smallest elements
B = maxk(A, k); % k largest elements
```
### Sum and Product
```matlab
s = sum(A); % Column sums
s = sum(A, 'all'); % Total sum
s = sum(A, dim); % Sum along dimension
s = cumsum(A); % Cumulative sum
p = prod(A); % Column products
p = prod(A, 'all'); % Total product
p = cumprod(A); % Cumulative product
```

View File

@@ -0,0 +1,544 @@
# GNU Octave Compatibility Reference
## Table of Contents
1. [Overview](#overview)
2. [Syntax Differences](#syntax-differences)
3. [Operator Differences](#operator-differences)
4. [Function Differences](#function-differences)
5. [Features Unique to Octave](#features-unique-to-octave)
6. [Features Missing in Octave](#features-missing-in-octave)
7. [Writing Compatible Code](#writing-compatible-code)
8. [Octave Packages](#octave-packages)
## Overview
GNU Octave is a free, open-source alternative to MATLAB with high compatibility. Most MATLAB scripts run in Octave with no or minimal modifications. However, there are some differences to be aware of.
### Installation
```bash
# macOS (Homebrew)
brew install octave
# Ubuntu/Debian
sudo apt install octave
# Fedora
sudo dnf install octave
# Windows
# Download installer from https://octave.org/download
```
### Running Octave
```bash
# Interactive mode
octave
# Run script
octave script.m
octave --eval "disp('Hello')"
# GUI mode
octave --gui
# Command-line only (no graphics)
octave --no-gui
octave-cli
```
## Syntax Differences
### Comments
```matlab
% MATLAB style (works in both)
% This is a comment
# Octave style (Octave only)
# This is also a comment in Octave
% For compatibility, always use %
```
### String Quotes
```matlab
% MATLAB: Single quotes only (char arrays)
str = 'Hello'; % char array
str = "Hello"; % string (R2017a+)
% Octave: Both work, but different behavior
str1 = 'Hello'; % char array, no escape sequences
str2 = "Hello\n"; % Interprets \n as newline
% For compatibility, use single quotes for char arrays
% Avoid double quotes with escape sequences
```
### Line Continuation
```matlab
% MATLAB style (works in both)
x = 1 + 2 + 3 + ...
4 + 5;
% Octave also accepts backslash
x = 1 + 2 + 3 + \
4 + 5;
% For compatibility, use ...
```
### Block Terminators
```matlab
% MATLAB style (works in both)
if condition
% code
end
for i = 1:10
% code
end
% Octave also accepts specific terminators
if condition
# code
endif
for i = 1:10
# code
endfor
while condition
# code
endwhile
% For compatibility, always use 'end'
```
### Function Definitions
```matlab
% MATLAB requires function in file with same name
% Octave allows command-line function definitions
% Octave command-line function
function y = f(x)
y = x^2;
endfunction
% For compatibility, define functions in .m files
```
## Operator Differences
### Increment/Decrement Operators
```matlab
% Octave has C-style operators (MATLAB does not)
x++; % x = x + 1
x--; % x = x - 1
++x; % Pre-increment
--x; % Pre-decrement
% For compatibility, use explicit assignment
x = x + 1;
x = x - 1;
```
### Compound Assignment
```matlab
% Octave supports (MATLAB does not)
x += 5; % x = x + 5
x -= 3; % x = x - 3
x *= 2; % x = x * 2
x /= 4; % x = x / 4
x ^= 2; % x = x ^ 2
% Element-wise versions
x .+= y;
x .-= y;
x .*= y;
x ./= y;
x .^= y;
% For compatibility, use explicit assignment
x = x + 5;
x = x .* y;
```
### Logical Operators
```matlab
% Both support
& | ~ && ||
% Short-circuit behavior difference:
% MATLAB: & and | short-circuit in if/while conditions
% Octave: Only && and || short-circuit
% For predictable behavior, use:
% && || for scalar short-circuit logic
% & | for element-wise operations
```
### Indexing After Expression
```matlab
% Octave allows indexing immediately after expression
result = sin(x)(1:10); % First 10 elements of sin(x)
value = func(arg).field; % Access field of returned struct
% MATLAB requires intermediate variable
temp = sin(x);
result = temp(1:10);
temp = func(arg);
value = temp.field;
% For compatibility, use intermediate variables
```
## Function Differences
### Built-in Functions
Most basic functions are compatible. Some differences:
```matlab
% Function name differences
% MATLAB Octave Alternative
% ------ ------------------
% inputname (not available)
% inputParser (partial support)
% validateattributes (partial support)
% Behavior differences in edge cases
% Check documentation for specific functions
```
### Random Number Generation
```matlab
% Both use Mersenne Twister by default
% Seed setting is similar
rng(42); % MATLAB
rand('seed', 42); % Octave (also accepts rng syntax)
% For compatibility
rng(42); % Works in modern Octave
```
### Graphics
```matlab
% Basic plotting is compatible
plot(x, y);
xlabel('X'); ylabel('Y');
title('Title');
legend('Data');
% Some advanced features differ
% - Octave uses gnuplot or Qt graphics
% - Some property names may differ
% - Animation/GUI features vary
% Test graphics code in both environments
```
### File I/O
```matlab
% Basic I/O is compatible
save('file.mat', 'x', 'y');
load('file.mat');
dlmread('file.txt');
dlmwrite('file.txt', data);
% MAT-file versions
save('file.mat', '-v7'); % Compatible format
save('file.mat', '-v7.3'); % HDF5 format (partial Octave support)
% For compatibility, use -v7 or -v6
```
## Features Unique to Octave
### do-until Loop
```matlab
% Octave only
do
x = x + 1;
until (x > 10)
% Equivalent MATLAB/compatible code
x = x + 1;
while x <= 10
x = x + 1;
end
```
### unwind_protect
```matlab
% Octave only - guaranteed cleanup
unwind_protect
% code that might error
result = risky_operation();
unwind_protect_cleanup
% always executed (like finally)
cleanup();
end_unwind_protect
% MATLAB equivalent
try
result = risky_operation();
catch
end
cleanup(); % Not guaranteed if error not caught
```
### Built-in Documentation
```matlab
% Octave supports Texinfo documentation in functions
function y = myfunction(x)
%% -*- texinfo -*-
%% @deftypefn {Function File} {@var{y} =} myfunction (@var{x})
%% Description of myfunction.
%% @end deftypefn
y = x.^2;
endfunction
```
### Package System
```matlab
% Octave Forge packages
pkg install -forge control
pkg load control
% List installed packages
pkg list
% For MATLAB compatibility, use equivalent toolboxes
% or include package functionality directly
```
## Features Missing in Octave
### Simulink
```matlab
% No Octave equivalent
% Simulink models (.slx, .mdl) cannot run in Octave
```
### MATLAB Toolboxes
```matlab
% Many toolbox functions not available
% Some have Octave Forge equivalents:
% MATLAB Toolbox Octave Forge Package
% --------------- --------------------
% Control System control
% Signal Processing signal
% Image Processing image
% Statistics statistics
% Optimization optim
% Check pkg list for available packages
```
### App Designer / GUIDE
```matlab
% MATLAB GUI tools not available in Octave
% Octave has basic UI functions:
uicontrol, uimenu, figure properties
% For cross-platform GUIs, consider:
% - Web-based interfaces
% - Qt (via Octave's Qt graphics)
```
### Object-Oriented Programming
```matlab
% Octave has partial classdef support
% Some features missing or behave differently:
% - Handle class events
% - Property validation
% - Some access modifiers
% For compatibility, use simpler OOP patterns
% or struct-based approaches
```
### Live Scripts
```matlab
% .mlx files are MATLAB-only
% Use regular .m scripts for compatibility
```
## Writing Compatible Code
### Detection
```matlab
function tf = isOctave()
tf = exist('OCTAVE_VERSION', 'builtin') ~= 0;
end
% Use for conditional code
if isOctave()
% Octave-specific code
else
% MATLAB-specific code
end
```
### Best Practices
```matlab
% 1. Use % for comments, not #
% Good
% This is a comment
% Avoid
# This is a comment (Octave only)
% 2. Use ... for line continuation
% Good
x = 1 + 2 + 3 + ...
4 + 5;
% Avoid
x = 1 + 2 + 3 + \
4 + 5;
% 3. Use 'end' for all blocks
% Good
if condition
code
end
% Avoid
if condition
code
endif
% 4. Avoid compound operators
% Good
x = x + 1;
% Avoid
x++;
x += 1;
% 5. Use single quotes for strings
% Good
str = 'Hello World';
% Avoid (escape sequence issues)
str = "Hello\nWorld";
% 6. Use intermediate variables for indexing
% Good
temp = func(arg);
result = temp(1:10);
% Avoid (Octave only)
result = func(arg)(1:10);
% 7. Save MAT-files in compatible format
save('data.mat', 'x', 'y', '-v7');
```
### Testing Compatibility
```bash
# Test in both environments
matlab -nodisplay -nosplash -r "run('test_script.m'); exit;"
octave --no-gui test_script.m
# Create test script
# test_script.m:
# try
# main_function();
# disp('Test passed');
# catch ME
# disp(['Test failed: ' ME.message]);
# end
```
## Octave Packages
### Installing Packages
```matlab
% Install from Octave Forge
pkg install -forge package_name
% Install from file
pkg install package_file.tar.gz
% Install from URL
pkg install 'http://example.com/package.tar.gz'
% Uninstall
pkg uninstall package_name
```
### Using Packages
```matlab
% Load package (required before use)
pkg load control
pkg load signal
pkg load image
% Load at startup (add to .octaverc)
pkg load control
% List loaded packages
pkg list
% Unload package
pkg unload control
```
### Common Packages
| Package | Description |
|---------|-------------|
| control | Control systems design |
| signal | Signal processing |
| image | Image processing |
| statistics | Statistical functions |
| optim | Optimization algorithms |
| io | Input/output functions |
| struct | Structure manipulation |
| symbolic | Symbolic math (via SymPy) |
| parallel | Parallel computing |
| netcdf | NetCDF file support |
### Package Management
```matlab
% Update all packages
pkg update
% Get package description
pkg describe package_name
% Check for updates
pkg list % Compare with Octave Forge website
```

672
references/programming.md Normal file
View File

@@ -0,0 +1,672 @@
# Programming Reference
## Table of Contents
1. [Scripts and Functions](#scripts-and-functions)
2. [Control Flow](#control-flow)
3. [Function Types](#function-types)
4. [Error Handling](#error-handling)
5. [Performance and Debugging](#performance-and-debugging)
6. [Object-Oriented Programming](#object-oriented-programming)
## Scripts and Functions
### Scripts
```matlab
% Scripts are .m files with MATLAB commands
% They run in the base workspace (share variables)
% Example: myscript.m
% This is a comment
x = 1:10;
y = x.^2;
plot(x, y);
title('My Plot');
% Run script
myscript; % Or: run('myscript.m')
```
### Functions
```matlab
% Functions have their own workspace
% Save in file with same name as function
% Example: myfunction.m
function y = myfunction(x)
%MYFUNCTION Brief description of function
% Y = MYFUNCTION(X) detailed description
%
% Example:
% y = myfunction(5);
%
% See also OTHERFUNCTION
y = x.^2;
end
% Multiple outputs
function [result1, result2] = multioutput(x)
result1 = x.^2;
result2 = x.^3;
end
% Variable arguments
function varargout = flexfun(varargin)
% varargin is cell array of inputs
% varargout is cell array of outputs
n = nargin; % Number of inputs
m = nargout; % Number of outputs
end
```
### Input Validation
```matlab
function result = validatedinput(x, options)
arguments
x (1,:) double {mustBePositive}
options.Normalize (1,1) logical = false
options.Scale (1,1) double {mustBePositive} = 1
end
result = x * options.Scale;
if options.Normalize
result = result / max(result);
end
end
% Usage
y = validatedinput([1 2 3], 'Normalize', true, 'Scale', 2);
% Common validators
% mustBePositive, mustBeNegative, mustBeNonzero
% mustBeInteger, mustBeNumeric, mustBeFinite
% mustBeNonNaN, mustBeReal, mustBeNonempty
% mustBeMember, mustBeInRange, mustBeGreaterThan
```
### Local Functions
```matlab
% Local functions appear after main function
% Only accessible within the same file
function result = mainfunction(x)
intermediate = helper1(x);
result = helper2(intermediate);
end
function y = helper1(x)
y = x.^2;
end
function y = helper2(x)
y = sqrt(x);
end
```
## Control Flow
### Conditional Statements
```matlab
% if-elseif-else
if condition1
% statements
elseif condition2
% statements
else
% statements
end
% Logical operators
% & - AND (element-wise)
% | - OR (element-wise)
% ~ - NOT
% && - AND (short-circuit, scalars)
% || - OR (short-circuit, scalars)
% == - Equal
% ~= - Not equal
% <, <=, >, >= - Comparisons
% Example
if x > 0 && y > 0
quadrant = 1;
elseif x < 0 && y > 0
quadrant = 2;
elseif x < 0 && y < 0
quadrant = 3;
else
quadrant = 4;
end
```
### Switch Statements
```matlab
switch expression
case value1
% statements
case {value2, value3} % Multiple values
% statements
otherwise
% default statements
end
% Example
switch dayOfWeek
case {'Saturday', 'Sunday'}
dayType = 'Weekend';
case {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'}
dayType = 'Weekday';
otherwise
dayType = 'Unknown';
end
```
### For Loops
```matlab
% Basic for loop
for i = 1:10
% statements using i
end
% Custom step
for i = 10:-1:1
% count down
end
% Loop over vector
for val = [1 3 5 7 9]
% val takes each value
end
% Loop over columns of matrix
for col = A
% col is a column vector
end
% Loop over cell array
for i = 1:length(C)
item = C{i};
end
```
### While Loops
```matlab
% Basic while loop
while condition
% statements
% Update condition
end
% Example
count = 0;
while count < 10
count = count + 1;
% Do something
end
```
### Loop Control
```matlab
% Break - exit loop immediately
for i = 1:100
if someCondition
break;
end
end
% Continue - skip to next iteration
for i = 1:100
if skipCondition
continue;
end
% Process i
end
% Return - exit function
function y = myfunction(x)
if x < 0
y = NaN;
return;
end
y = sqrt(x);
end
```
## Function Types
### Anonymous Functions
```matlab
% Create inline function
f = @(x) x.^2 + 2*x + 1;
g = @(x, y) x.^2 + y.^2;
% Use
y = f(5); % 36
z = g(3, 4); % 25
% With captured variables
a = 2;
h = @(x) a * x; % Captures current value of a
y = h(5); % 10
a = 3; % Changing a doesn't affect h
y = h(5); % Still 10
% No arguments
now_fn = @() datestr(now);
timestamp = now_fn();
% Pass to other functions
result = integral(f, 0, 1);
```
### Nested Functions
```matlab
function result = outerfunction(x)
y = x.^2; % Shared with nested functions
function z = nestedfunction(a)
z = y + a; % Can access y from outer scope
end
result = nestedfunction(10);
end
```
### Function Handles
```matlab
% Create handle to existing function
h = @sin;
y = h(pi/2); % 1
% From string
h = str2func('cos');
% Get function name
name = func2str(h);
% Get handles to local functions
handles = localfunctions;
% Function info
info = functions(h);
```
### Callbacks
```matlab
% Using function handles as callbacks
% Timer example
t = timer('TimerFcn', @myCallback, 'Period', 1);
start(t);
function myCallback(~, ~)
disp(['Time: ' datestr(now)]);
end
% With anonymous function
t = timer('TimerFcn', @(~,~) disp('Tick'), 'Period', 1);
% GUI callbacks
uicontrol('Style', 'pushbutton', 'Callback', @buttonPressed);
```
## Error Handling
### Try-Catch
```matlab
try
% Code that might error
result = riskyOperation();
catch ME
% Handle error
disp(['Error: ' ME.message]);
disp(['Identifier: ' ME.identifier]);
% Optionally rethrow
rethrow(ME);
end
% Catch specific errors
try
result = operation();
catch ME
switch ME.identifier
case 'MATLAB:divideByZero'
result = Inf;
case 'MATLAB:nomem'
rethrow(ME);
otherwise
result = NaN;
end
end
```
### Throwing Errors
```matlab
% Simple error
error('Something went wrong');
% With identifier
error('MyPkg:InvalidInput', 'Input must be positive');
% With formatting
error('MyPkg:OutOfRange', 'Value %f is out of range [%f, %f]', val, lo, hi);
% Create and throw exception
ME = MException('MyPkg:Error', 'Error message');
throw(ME);
% Assertion
assert(condition, 'Message if false');
assert(x > 0, 'MyPkg:NotPositive', 'x must be positive');
```
### Warnings
```matlab
% Issue warning
warning('This might be a problem');
warning('MyPkg:Warning', 'Warning message');
% Control warnings
warning('off', 'MyPkg:Warning'); % Disable specific warning
warning('on', 'MyPkg:Warning'); % Enable
warning('off', 'all'); % Disable all
warning('on', 'all'); % Enable all
% Query warning state
s = warning('query', 'MyPkg:Warning');
% Temporarily disable
origState = warning('off', 'MATLAB:nearlySingularMatrix');
% ... code ...
warning(origState);
```
## Performance and Debugging
### Timing
```matlab
% Simple timing
tic;
% ... code ...
elapsed = toc;
% Multiple timers
t1 = tic;
% ... code ...
elapsed1 = toc(t1);
% CPU time
t = cputime;
% ... code ...
cpuElapsed = cputime - t;
% Profiler
profile on;
myfunction();
profile viewer; % GUI to analyze results
p = profile('info'); % Get programmatic results
profile off;
```
### Memory
```matlab
% Memory info
[user, sys] = memory; % Windows only
whos; % Variable sizes
% Clear variables
clear x y z;
clear all; % All variables (use sparingly)
clearvars -except x y; % Keep specific variables
```
### Debugging
```matlab
% Set breakpoints (in editor or programmatically)
dbstop in myfunction at 10
dbstop if error
dbstop if warning
dbstop if naninf % Stop on NaN or Inf
% Step through code
dbstep % Next line
dbstep in % Step into function
dbstep out % Step out of function
dbcont % Continue execution
dbquit % Quit debugging
% Clear breakpoints
dbclear all
% Examine state
dbstack % Call stack
whos % Variables
```
### Vectorization Tips
```matlab
% AVOID loops when possible
% Slow:
for i = 1:n
y(i) = x(i)^2;
end
% Fast:
y = x.^2;
% Element-wise operations (use . prefix)
y = a .* b; % Element-wise multiply
y = a ./ b; % Element-wise divide
y = a .^ b; % Element-wise power
% Built-in functions operate on arrays
y = sin(x); % Apply to all elements
s = sum(x); % Sum all
m = max(x); % Maximum
% Logical indexing instead of find
% Slow:
idx = find(x > 0);
y = x(idx);
% Fast:
y = x(x > 0);
% Preallocate arrays
% Slow:
y = [];
for i = 1:n
y(i) = compute(i);
end
% Fast:
y = zeros(1, n);
for i = 1:n
y(i) = compute(i);
end
```
### Parallel Computing
```matlab
% Parallel for loop
parfor i = 1:n
results(i) = compute(i);
end
% Note: parfor has restrictions
% - Iterations must be independent
% - Variable classifications (sliced, broadcast, etc.)
% Start parallel pool
pool = parpool; % Default cluster
pool = parpool(4); % 4 workers
% Delete pool
delete(gcp('nocreate'));
% Parallel array operations
spmd
% Each worker executes this block
localData = myData(labindex);
result = process(localData);
end
```
## Object-Oriented Programming
### Class Definition
```matlab
% In file MyClass.m
classdef MyClass
properties
PublicProp
end
properties (Access = private)
PrivateProp
end
properties (Constant)
ConstProp = 42
end
methods
% Constructor
function obj = MyClass(value)
obj.PublicProp = value;
end
% Instance method
function result = compute(obj, x)
result = obj.PublicProp * x;
end
end
methods (Static)
function result = staticMethod(x)
result = x.^2;
end
end
end
```
### Using Classes
```matlab
% Create object
obj = MyClass(10);
% Access properties
val = obj.PublicProp;
obj.PublicProp = 20;
% Call methods
result = obj.compute(5);
result = compute(obj, 5); % Equivalent
% Static method
result = MyClass.staticMethod(3);
% Constant property
val = MyClass.ConstProp;
```
### Inheritance
```matlab
classdef DerivedClass < BaseClass
properties
ExtraProp
end
methods
function obj = DerivedClass(baseVal, extraVal)
% Call superclass constructor
obj@BaseClass(baseVal);
obj.ExtraProp = extraVal;
end
% Override method
function result = compute(obj, x)
% Call superclass method
baseResult = compute@BaseClass(obj, x);
result = baseResult + obj.ExtraProp;
end
end
end
```
### Handle vs Value Classes
```matlab
% Value class (default) - copy semantics
classdef ValueClass
properties
Data
end
end
a = ValueClass();
a.Data = 1;
b = a; % b is a copy
b.Data = 2; % a.Data is still 1
% Handle class - reference semantics
classdef HandleClass < handle
properties
Data
end
end
a = HandleClass();
a.Data = 1;
b = a; % b references same object
b.Data = 2; % a.Data is now 2
```
### Events and Listeners
```matlab
classdef EventClass < handle
events
DataChanged
end
properties
Data
end
methods
function set.Data(obj, value)
obj.Data = value;
notify(obj, 'DataChanged');
end
end
end
% Usage
obj = EventClass();
listener = addlistener(obj, 'DataChanged', @(src, evt) disp('Data changed!'));
obj.Data = 42; % Triggers event
```

View File

@@ -0,0 +1,433 @@
# Python Integration Reference
## Table of Contents
1. [Calling Python from MATLAB](#calling-python-from-matlab)
2. [Data Type Conversion](#data-type-conversion)
3. [Working with Python Objects](#working-with-python-objects)
4. [Calling MATLAB from Python](#calling-matlab-from-python)
5. [Common Workflows](#common-workflows)
## Calling Python from MATLAB
### Setup
```matlab
% Check Python configuration
pyenv
% Set Python version (before calling any Python)
pyenv('Version', '/usr/bin/python3');
pyenv('Version', '3.10');
% Check if Python is available
pe = pyenv;
disp(pe.Version);
disp(pe.Executable);
```
### Basic Python Calls
```matlab
% Call built-in functions with py. prefix
result = py.len([1, 2, 3, 4]); % 4
result = py.sum([1, 2, 3, 4]); % 10
result = py.max([1, 2, 3, 4]); % 4
result = py.abs(-5); % 5
% Create Python objects
pyList = py.list({1, 2, 3});
pyDict = py.dict(pyargs('a', 1, 'b', 2));
pySet = py.set({1, 2, 3});
pyTuple = py.tuple({1, 2, 3});
% Call module functions
result = py.math.sqrt(16);
result = py.os.getcwd();
wrapped = py.textwrap.wrap('This is a long string');
```
### Import and Use Modules
```matlab
% Import module
np = py.importlib.import_module('numpy');
pd = py.importlib.import_module('pandas');
% Use module
arr = np.array({1, 2, 3, 4, 5});
result = np.mean(arr);
% Alternative: direct py. syntax
arr = py.numpy.array({1, 2, 3, 4, 5});
result = py.numpy.mean(arr);
```
### Run Python Code
```matlab
% Run Python statements
pyrun("x = 5")
pyrun("y = x * 2")
result = pyrun("z = y + 1", "z");
% Run Python file
pyrunfile("script.py");
result = pyrunfile("script.py", "output_variable");
% Run with input variables
x = 10;
result = pyrun("y = x * 2", "y", x=x);
```
### Keyword Arguments
```matlab
% Use pyargs for keyword arguments
result = py.sorted({3, 1, 4, 1, 5}, pyargs('reverse', true));
% Multiple keyword arguments
df = py.pandas.DataFrame(pyargs( ...
'data', py.dict(pyargs('A', {1, 2, 3}, 'B', {4, 5, 6})), ...
'index', {'x', 'y', 'z'}));
```
## Data Type Conversion
### MATLAB to Python
| MATLAB Type | Python Type |
|-------------|-------------|
| double, single | float |
| int8, int16, int32, int64 | int |
| uint8, uint16, uint32, uint64 | int |
| logical | bool |
| char, string | str |
| cell array | list |
| struct | dict |
| numeric array | numpy.ndarray (if numpy available) |
```matlab
% Automatic conversion examples
py.print(3.14); % float
py.print(int32(42)); % int
py.print(true); % bool (True)
py.print("hello"); % str
py.print({'a', 'b'}); % list
% Explicit conversion to Python types
pyInt = py.int(42);
pyFloat = py.float(3.14);
pyStr = py.str('hello');
pyList = py.list({1, 2, 3});
pyDict = py.dict(pyargs('key', 'value'));
```
### Python to MATLAB
```matlab
% Convert Python types to MATLAB
matlabDouble = double(py.float(3.14));
matlabInt = int64(py.int(42));
matlabChar = char(py.str('hello'));
matlabString = string(py.str('hello'));
matlabCell = cell(py.list({1, 2, 3}));
% Convert numpy arrays
pyArr = py.numpy.array({1, 2, 3, 4, 5});
matlabArr = double(pyArr);
% Convert pandas DataFrame to MATLAB table
pyDf = py.pandas.read_csv('data.csv');
matlabTable = table(pyDf); % Requires pandas2table or similar
% Manual DataFrame conversion
colNames = cell(pyDf.columns.tolist());
data = cell(pyDf.values.tolist());
T = cell2table(data, 'VariableNames', colNames);
```
### Array Conversion
```matlab
% MATLAB array to numpy
matlabArr = [1 2 3; 4 5 6];
pyArr = py.numpy.array(matlabArr);
% numpy to MATLAB
pyArr = py.numpy.random.rand(int64(3), int64(4));
matlabArr = double(pyArr);
% Note: numpy uses row-major (C) order, MATLAB uses column-major (Fortran)
% Transposition may be needed for correct layout
```
## Working with Python Objects
### Object Methods and Properties
```matlab
% Call methods
pyList = py.list({3, 1, 4, 1, 5});
pyList.append(9);
pyList.sort();
% Access properties/attributes
pyStr = py.str('hello world');
upper = pyStr.upper();
words = pyStr.split();
% Check attributes
methods(pyStr) % List methods
fieldnames(pyDict) % List keys
```
### Iterating Python Objects
```matlab
% Iterate over Python list
pyList = py.list({1, 2, 3, 4, 5});
for item = py.list(pyList)
disp(item{1});
end
% Convert to cell and iterate
items = cell(pyList);
for i = 1:length(items)
disp(items{i});
end
% Iterate dict keys
pyDict = py.dict(pyargs('a', 1, 'b', 2, 'c', 3));
keys = cell(pyDict.keys());
for i = 1:length(keys)
key = keys{i};
value = pyDict{key};
fprintf('%s: %d\n', char(key), int64(value));
end
```
### Error Handling
```matlab
try
result = py.some_module.function_that_might_fail();
catch ME
if isa(ME, 'matlab.exception.PyException')
disp('Python error occurred:');
disp(ME.message);
else
rethrow(ME);
end
end
```
## Calling MATLAB from Python
### Setup MATLAB Engine
```python
# Install MATLAB Engine API for Python
# From MATLAB: cd(fullfile(matlabroot,'extern','engines','python'))
# Then: python setup.py install
import matlab.engine
# Start MATLAB engine
eng = matlab.engine.start_matlab()
# Or connect to shared session (MATLAB: matlab.engine.shareEngine)
eng = matlab.engine.connect_matlab()
# List available sessions
matlab.engine.find_matlab()
```
### Call MATLAB Functions
```python
import matlab.engine
eng = matlab.engine.start_matlab()
# Call built-in functions
result = eng.sqrt(16.0)
result = eng.sin(3.14159 / 2)
# Multiple outputs
mean_val, std_val = eng.std([1, 2, 3, 4, 5], nargout=2)
# Matrix operations
A = matlab.double([[1, 2], [3, 4]])
B = eng.inv(A)
C = eng.mtimes(A, B) # Matrix multiplication
# Call custom function (must be on MATLAB path)
result = eng.myfunction(arg1, arg2)
# Cleanup
eng.quit()
```
### Data Conversion (Python to MATLAB)
```python
import matlab.engine
import numpy as np
eng = matlab.engine.start_matlab()
# Python to MATLAB types
matlab_double = matlab.double([1.0, 2.0, 3.0])
matlab_int = matlab.int32([1, 2, 3])
matlab_complex = matlab.double([1+2j, 3+4j], is_complex=True)
# 2D array
matlab_matrix = matlab.double([[1, 2, 3], [4, 5, 6]])
# numpy to MATLAB
np_array = np.array([[1, 2], [3, 4]], dtype=np.float64)
matlab_array = matlab.double(np_array.tolist())
# Call MATLAB with numpy data
result = eng.sum(matlab.double(np_array.flatten().tolist()))
```
### Async Calls
```python
import matlab.engine
eng = matlab.engine.start_matlab()
# Asynchronous call
future = eng.sqrt(16.0, background=True)
# Do other work...
# Get result when ready
result = future.result()
# Check if done
if future.done():
result = future.result()
# Cancel if needed
future.cancel()
```
## Common Workflows
### Using Python Libraries in MATLAB
```matlab
% Use scikit-learn from MATLAB
sklearn = py.importlib.import_module('sklearn.linear_model');
% Prepare data
X = rand(100, 5);
y = X * [1; 2; 3; 4; 5] + randn(100, 1) * 0.1;
% Convert to Python/numpy
X_py = py.numpy.array(X);
y_py = py.numpy.array(y);
% Train model
model = sklearn.LinearRegression();
model.fit(X_py, y_py);
% Get coefficients
coefs = double(model.coef_);
intercept = double(model.intercept_);
% Predict
y_pred = double(model.predict(X_py));
```
### Using MATLAB in Python Scripts
```python
import matlab.engine
import numpy as np
# Start MATLAB
eng = matlab.engine.start_matlab()
# Use MATLAB's optimization
def matlab_fmincon(objective, x0, A, b, Aeq, beq, lb, ub):
"""Wrapper for MATLAB's fmincon."""
# Convert to MATLAB types
x0_m = matlab.double(x0.tolist())
A_m = matlab.double(A.tolist()) if A is not None else matlab.double([])
b_m = matlab.double(b.tolist()) if b is not None else matlab.double([])
# Call MATLAB (assuming objective is a MATLAB function)
x, fval = eng.fmincon(objective, x0_m, A_m, b_m, nargout=2)
return np.array(x).flatten(), fval
# Use MATLAB's plotting
def matlab_plot(x, y, title_str):
"""Create plot using MATLAB."""
eng.figure(nargout=0)
eng.plot(matlab.double(x.tolist()), matlab.double(y.tolist()), nargout=0)
eng.title(title_str, nargout=0)
eng.saveas(eng.gcf(), 'plot.png', nargout=0)
eng.quit()
```
### Sharing Data Between MATLAB and Python
```matlab
% Save data for Python
data = rand(100, 10);
labels = randi([0 1], 100, 1);
save('data_for_python.mat', 'data', 'labels');
% In Python:
% import scipy.io
% mat = scipy.io.loadmat('data_for_python.mat')
% data = mat['data']
% labels = mat['labels']
% Load data from Python (saved with scipy.io.savemat)
loaded = load('data_from_python.mat');
data = loaded.data;
labels = loaded.labels;
% Alternative: use CSV for simple data exchange
writematrix(data, 'data.csv');
% Python: pd.read_csv('data.csv')
% Python writes: df.to_csv('results.csv')
results = readmatrix('results.csv');
```
### Using Python Packages Not Available in MATLAB
```matlab
% Example: Use Python's requests library
requests = py.importlib.import_module('requests');
% Make HTTP request
response = requests.get('https://api.example.com/data');
status = int64(response.status_code);
if status == 200
data = response.json();
% Convert to MATLAB structure
dataStruct = struct(data);
end
% Example: Use Python's PIL/Pillow for advanced image processing
PIL = py.importlib.import_module('PIL.Image');
% Open image
img = PIL.open('image.png');
% Resize
img_resized = img.resize(py.tuple({int64(256), int64(256)}));
% Save
img_resized.save('image_resized.png');
```