Skip to content
MATLAB

文件 I/O

读写 .mat、.csv 和文本文件。

#file#io#csv

Code

matlab
% Save/Load .mat files
A = rand(5);
save('data.mat', 'A');   % save variable A
load('data.mat');        % load into workspace

% CSV
M = csvread('data.csv');       % read (legacy)
M = readmatrix('data.csv');    % read (modern)
writematrix(M, 'out.csv');     % write

% Text files
fid = fopen('data.txt', 'r');
content = fscanf(fid, '%s');
fclose(fid);

% Table (recommended for tabular data)
T = readtable('data.csv');
T.Height(1) = 175;  % modify column
writetable(T, 'out.csv');