当前位置:网站首页>Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
2022-07-06 22:12:00 【Entertainment first 2091】
MATLAB Read text files with mixed numeric strings txt Data in
The goal is
Extract the data in the text below in multiple arrays


Introduce
It was introduced before in txt In the text , The content is in the case of regular columns , You can refer to : Data processing skills (5):MATLAB Read txt Data in . The last introduction mainly deals with the following two figures , Arranged in columns .

This time, the data is more complex , Read this article directly , Each column is not so neat . It mainly deals with three situations : Pure number 、 character + Numbers 、 character + Numbers + character + The situation of numbers .
① Pure digital situation
Text file to be read :

Determine the file path
Judge whether the input text file path is correct , If you enter an incorrect file name , Will prompt “ Wrong confidence + File opening failure .” A hint of .

After entering the correct file path , End of reading data , There will be “ Successfully read file data ” A hint of .

matlab The result of reading data
Open... In the workspace mdata, double-click mdata Of data Variable , You can see the read pure digital data .

because matlab The table of displays by default 4 Decimal place , So it looks different . actually matlab The calculation does not just show 4 Decimal place , It can be calculated normally .
Code block
%% purenum
% establish data Array
mdata.data = [];
FileLoc = "purenum.txt"; % The path where the text file is located , If in the same folder , Just write the file name
[datafid,datamess] = fopen(FileLoc,"r"); % open the text file , Read only mode on
if datafid==-1
% Successfully opened , Returns a non negative number ; Open failed, return -1
disp(datamess);
disp(" File opening failure .");
else
tline = fgets(datafid); % Read the first line first
while tline~=-1 % When a row of data is -1, Note that all lines of the file are traversed
data123 = sscanf(tline,'%f',3); % Find the point coordinates ( Serial number ,x,y,z), Remember to transpose
mdata.data=[mdata.data;data123']; % Stored in array
tline = fgets(datafid); % iteration , Read the next line
end
fclose(datafid); % Close file
disp(" Successfully read file data ."); % Tips
end
② The beginning of the text , The situation after the number
Text file to be read

matlab The result of reading data

Code block
%% charnum
% Create three arrays
mdata.VArray = [];
mdata.rgb = [];
mdata.point = [];
FileLoc = "charnum2.txt"; % The path where the text file is located , If in the same folder , Just write the file name
[datafid,datamess] = fopen(FileLoc,"r"); % open the text file , Read only mode on
if datafid==-1
% Successfully opened , Returns a non negative number ; Open failed, return -1
disp(datamess);
disp(" File opening failure .");
else
tline = fgets(datafid); % Read the first line first
while tline~=-1 % When a row of data is -1, Note that all lines of the file are traversed
% Judge the first word at the beginning of the line
[ln,~,~,n] = sscanf(tline,'%s',1); % Process this row of data
if ln=="Vertex"
tline1=tline(n:end); % truncation - Delete Vertex
Vxyz = sscanf(tline1,'%f',3); % Find the point coordinates ( Serial number ,x,y,z), Remember to transpose
mdata.VArray=[mdata.VArray;Vxyz']; % Stored in array
end
if ln=="rgb"
tline1=tline(n:end); % truncation - Delete Vertex
Vxyz = sscanf(tline1,'%f',3); % Find the point coordinates ( Serial number ,x,y,z), Remember to transpose
mdata.rgb=[mdata.rgb;Vxyz']; % Stored in array
end
if ln=="point"
tline1=tline(n:end); % truncation - Delete Vertex
Vxyz = sscanf(tline1,'%f',3); % Find the point coordinates ( Serial number ,x,y,z), Remember to transpose
mdata.point=[mdata.point;Vxyz']; % Stored in array
end
tline = fgets(datafid); % iteration
end
fclose(datafid); % Close file
disp(" Successfully read file data ."); % Tips
end
③ A line of words mixed with numbers
Files to read :

matlab Read data results :

Code block
%% charnumcharnum
% Create three arrays
mdata.VArray = [];
mdata.rgb = [];
mdata.point = [];
FileLoc = "charnumcharnum.txt"; % The path where the text file is located , If in the same folder , Just write the file name
[datafid,datamess] = fopen(FileLoc,"r"); % open the text file , Read only mode on
if datafid==-1
% Successfully opened , Returns a non negative number ; Open failed, return -1
disp(datamess);
disp(" File opening failure .");
else
tline = fgets(datafid); % Read the first line first
while tline~=-1 % When a row of data is -1, Note that all lines of the file are traversed
% Judge the first word at the beginning of the line
[ln,~,~,n] = sscanf(tline,'%s',1); % Process this row of data
if ln=="Vertex"
tline1=tline(n:end); % truncation - Delete Vertex
Vxyz = sscanf(tline1,'%f',3); % Find the point coordinates ( Serial number ,x,y,z), Remember to transpose
mdata.VArray=[mdata.VArray;Vxyz']; % Stored in array
% look for rgb
k1 = strfind(tline, 'rgb');
if k1>0
k1=k1+3; % Find and delete rgb Last position
tline1=tline(k1:end);
rgb = sscanf(tline1,'%f',3); % find rgb
mdata.rgb=[mdata.rgb;rgb']; % Stored in an array
end
% look for point
k2 = strfind(tline, 'point');
if k2>0
k2=k2+5; % Find and delete point Last position
tline2=tline(k2:end);
point = sscanf(tline2,'%f',3); % find point
mdata.point=[mdata.point;point']; % Stored in an array
end
end
tline = fgets(datafid); % iteration
end
fclose(datafid); % Close file
disp(" Successfully read file data ."); % Tips
end
ending
Through this introduction , It can be used very quickly matlab Extract data from text files , For follow-up matlab Handle . combination Data processing skills (5):MATLAB Read txt Data in It should be able to solve many problems of reading data .
边栏推荐
- 基于LM317的可调直流电源
- GPS from getting started to giving up (XX), antenna offset
- GPS从入门到放弃(十四)、电离层延时
- 华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
- 新入职一家公司需要去实践和注意的内容
- BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
- Aggregate function with key in spark
- Classic sql50 questions
- Checkpoint of RDD in spark
- Maximum product of three numbers in question 628 of Li Kou
猜你喜欢

GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)

Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others

MPLS experiment

GNN, please deepen your network layer~
![[daily] win10 system setting computer never sleeps](/img/94/15f5a368e395b6948f409c5f6fc871.jpg)
[daily] win10 system setting computer never sleeps

Checkpoint of RDD in spark

GPS from entry to abandonment (XIV), ionospheric delay

GPS from getting started to giving up (19), precise ephemeris (SP3 format)

C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4

GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
随机推荐
Four data streams of grpc
Oracle-控制文件及日志文件的管理
The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
二叉(搜索)树的最近公共祖先 ●●
Why rdd/dataset is needed in spark
NPM run dev start project error document is not defined
【sdx62】WCN685X将bdwlan.bin和bdwlan.txt相互转化操作方法
GPS from getting started to giving up (12), Doppler constant speed
Codeforces Round #274 (Div. 2) –A Expression
i.mx6ull搭建boa服务器详解及其中遇到的一些问题
Oracle性能分析3:TKPROF简介
AI 企业多云存储架构实践 | 深势科技分享
Shortcut keys in the terminal
Bat script learning (I)
GPS from getting started to giving up (XI), differential GPS
微信红包封面小程序源码-后台独立版-带测评积分功能源码
Basic introduction of figure
2500个常用中文字符 + 130常用中英文字符
[MySQL] online DDL details
【10点公开课】:视频质量评价基础与实践