MATLAB Quick Start (9)

1. Plain text file

1.1 Write to file

Use the dlmwrite command to save the matrix a to the plain text file data2.txt.

a=[1 1 1;2 2 2];
dlmwrite('data1.txt',a)

Use the fprint command to save to the plain text file data2.txt.

fid=fopen('data2.txt','w');
a=normrnd(0,1,10,20);
fprintf(fid,'%f\n',a');
fclose(fid);

 1.2 Reading files

Use the load or textread command to read.

b=load('data1.txt')
c=textread('data2.txt')

2. CSV file and Excel file

Write a matrix to a csv file:

d=rand(5,10);
csvwrite('data3.csv',d)

Read the csv file:

e=csvread('data3.csv')

For Excel files, the reading and writing methods are similar to csv, and the writing and reading commands are xlswrite and xlsread respectively; because the reading speed of Excel files is relatively slow, we often use csv files.

3. Image file

Use the imread command to read a bmp image file, and then use the imwrite command to convert it into a relatively small file.

a1=imread('data4.bmp');
subplot(1,2,1);imshow(a1)
imwrite(a1,'data5.jpg');
subplot(1,2,2);imshow('data5.jpg')

 

 4. Video files

A video file is essentially composed of multiple frames of images with a certain size, order, and format. Video is to continuously display multiple frames of still images to achieve a dynamic effect.

Use the VideoReader command to read a video file and save each frame in the video as a jpg file.

ob=VideoReader('test.avi') %读取视频文件对象
get(ob)%获取视频对象的参数
n=ob.NumberOfFrame;  %获取视频的总帧数
for i=1:n
    a=read(ob,i); %读取视频对象的第i帧
    imshow(a)  %显示第i帧图像
    str=['zpic\',int2str(i),'.jpg']; %构造文件名的字符串,目录zpic要提前建好
    imwrite(a,str); %把第i帧保存到jpg文件
end

Note: The folder must be created in advance.

For more content, please pay attention to the official account " Talking with You ", and reply to the keyword " Getting Started " in the background, and you can get the complete PDF document.

 

 

 

Guess you like

Origin blog.csdn.net/m0_64087341/article/details/125352608