Data Import and Export
## Learning Objectives
- Import data from files
- Export data to files
- Work with different file formats
- Handle missing data
## Text Files
### Reading Text Files
```matlab
% Read entire file as text
fid = fopen('data.txt', 'r');
text = fread(fid, '*char')';
fclose(fid);
```
### Reading with fscanf
```matlab
% Formatted reading
fid = fopen('data.txt', 'r');
data = fscanf(fid, '%f %f %f', [3, inf]);
fclose(fid);
data = data'; % Transpose to get rows as records
```
### Reading Line by Line
```matlab
fid = fopen('data.txt', 'r');
while ~feof(fid)
line = fgetl(fid);
disp(line)
end
fclose(fid);
```
## CSV Files
### Reading CSV
```matlab
% Basic CSV reading
data = readmatrix('data.csv');
% With header row
data = readmatrix('data.csv', 'NumHeaderLines', 1);
% Specify range
data = readmatrix('data.csv', 'Range', 'A2:D10');
```
### Reading with readtable
```matlab
% readtable returns a table
T = readtable('data.csv');
% Access by variable name
T.Name
T.Age
T.Height
% Access by index
T{1, :}
```
### Writing CSV
```matlab
% Write matrix
M = [1, 2, 3; 4, 5, 6];
writematrix(M, 'output.csv');
% Write table
T = table(['John'; 'Jane'], [25; 30], [170; 165], ...
'VariableNames', {'Name', 'Age', 'Height'});
writetable(T, 'people.csv');
```
## Excel Files
### Reading Excel
```matlab
% Read entire sheet
data = readmatrix('data.xlsx');
% Read specific sheet
data = readmatrix('data.xlsx', 'Sheet', 'Sheet2');
% Read as table
T = readtable('data.xlsx');
```
### Writing Excel
```matlab
% Write matrix
M = magic(5);
writematrix(M, 'magic.xlsx');
% Write table
writetable(T, 'output.xlsx');
% Append to existing file
writetable(T, 'existing.xlsx', 'WriteMode', 'append');
```
## Delimited Files
### readmatrix Options
```matlab
% Tab-delimited
data = readmatrix('data.txt', 'Delimiter', '\t');
% Semicolon-delimited
data = readmatrix('data.txt', 'Delimiter', ';');
% With multiple delimiters
data = readmatrix('data.txt', 'Delimiter', {',', ';', '\t'});
```
### writematrix Options
```matlab
% Tab-delimited
writematrix(M, 'output.txt', 'Delimiter', '\t');
% Specify precision
writematrix(M, 'output.txt', 'Precision', 4);
```
## Native Format (.mat)
### Saving Variables
```matlab
% Save all workspace variables
save('data.mat')
% Save specific variables
save('data.mat', 'x', 'y', 'z')
% Save with compression
save('data.mat', 'x', 'y', '-v7')
% Save with no compression
save('data.mat', 'x', 'y', '-v6')
```
### Loading Variables
```matlab
% Load all variables
load('data.mat')
% Load specific variables
load('data.mat', 'x', 'y')
% Load to structure
S = load('data.mat');
S.x
S.y
```
## Tables
### Creating Tables
```matlab
% From arrays
Age = [25, 30, 35]';
Height = [170, 165, 180]';
Name = {'John'; 'Jane'; 'Bob'}';
T = table(Name, Age, Height);
```
### Table Operations
```matlab
% Display first few rows
head(T)
% Display last few rows
tail(T)
% Get summary
summary(T)
% Add row
T(end+1, :) = {'Mike', 28, 175};
% Remove row
T(2, :) = [];
```
### Table Indexing
```matlab
% By row/column index
T{1, 2} % Age of first person
T{2, 'Name'} % Name of second person
T.Age % Age column as array
T(:, {'Name', 'Age'}) % Specific columns
```
## Missing Data
### Detecting Missing Values
```matlab
% NaN in numeric data
data = [1, NaN, 3, 4, NaN];
isnan(data) % [0, 1, 0, 0, 1]
any(isnan(data)) % true
```
### Handling Missing Values
```matlab
data = [1, NaN, 3, 4, NaN];
% Remove NaN
clean = data(~isnan(data)); % [1, 3, 4]
% Fill NaN with value
filled = fillmissing(data, 'constant', 0);
% Fill with interpolation
filled = fillmissing(data, 'linear');
% Fill with previous value
filled = fillmissing(data, 'previous');
```
### Table with Missing Data
```matlab
T = readtable('data.csv');
% Find missing values
ismissing(T)
% Remove rows with missing
T = rmmissing(T);
% Fill missing
T = fillmissing(T, 'constant', 0);
```
## Low-Level File I/O
### File Opening Modes
| Mode | Description |
|------|-------------|
| `r` | Read (default) |
| `w` | Write (create/truncate) |
| `a` | Append (create/write at end) |
| `r+` | Read and write |
| `w+` | Read and write (create/truncate) |
### Writing to File
```matlab
fid = fopen('output.txt', 'w');
fprintf(fid, 'Hello, %s!\n', 'World');
fprintf(fid, 'Value: %d\n', 42);
fclose(fid);
```
### Reading from File
```matlab
fid = fopen('data.txt', 'r');
name = fscanf(fid, '%s', 1);
value = fscanf(fid, '%d', 1);
fclose(fid);
```
## Binary Files
### Reading Binary
```matlab
fid = fopen('data.bin', 'r');
data = fread(fid, 100, 'float32');
fclose(fid);
```
### Writing Binary
```matlab
fid = fopen('data.bin', 'w');
fwrite(fid, myData, 'float32');
fclose(fid);
```
## XML Files
```matlab
% Parse XML
tree = xmlread('data.xml');
% Navigate and extract data
root = tree.getDocumentElement;
child = root.getFirstChildElement;
```
## Image Files
```matlab
% Read image
img = imread('image.png');
% Display image
imshow(img)
% Write image
imwrite(img, 'output.png');
% Write JPEG with quality
imwrite(img, 'output.jpg', 'Quality', 90);
```
## Importing Examples
### Import Fixed-Width Text
```matlab
% Define format
format = '%2s %5f %4f %3f';
% Read with textscan
fid = fopen('data.txt', 'r');
C = textscan(fid, format);
fclose(fid);
```
### Import with Header
```matlab
opts = detectImportOptions('data.csv');
opts.VariableNamesLine = 1;
opts.DataLines = 2:10;
T = readtable('data.csv', opts);
```
## Summary
- `readmatrix()` for numeric data in delimited files
- `readtable()` for tabular data with headers
- `save()`/`load()` for MATLAB .mat files
- Use `'Delimiter'` option for different separators
- Handle missing data with `isnan()`, `rmmissing()`, `fillmissing()`
- Use `fopen()`/`fprintf()`/`fscanf()` for custom file formats
- `imread()`/`imwrite()` for image files
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →