Read text file data

5 次查看(过去 30 天)
Thanh Cao
Thanh Cao 2018-4-16
I have a text file contain the following data:
20120906092900;55.05;87.82;87.82;84.28CRLF
20120906093000;55.05;87.81;87.81;84.28CRLF
20120906093100;55.05;87.82;87.82;84.29CRLF
20120906093200;55.04;87.82;87.82;84.29CRLF
20120906093300;55.04;87.81;87.81;84.28CRLF
20120906093400;55.04;87.82;87.82;84.28CRLF
20120906093800;55.03;87.81;87.81;84.28CRLF
20120906093900;55.03;87.81;87.81;84.27CRLF
20120906094000;55.02;87.81;87.81;84.28CRLF
20120906094100;55.02;87.81;87.81;84.29CRLF
20120906094200;55.02;87.81;87.81;84.29CRLF
20120906094300;55.01;87.82;87.82;84.28CRLF
20120906094700;55.00;87.81;87.81;84.28CRLF
20120906094800;55.00;87.82;87.82;84.29CRLF
.
.
.
.
.
.
I want to open this text file and read the first field (before the semi column) as string and store them in the string array.
The next four numbers will be stored them in array of 4 column floating number.
Array A is a string array, array B is a floating number array
A = 20120906092900 B = 55.05 87.82 87.82 84.28
20120906093000 55.05 87.81 87.81 84.28
20120906093100 55.05 87.82;87.82;84.29
20120906093200 55.04 87.82 87.82 84.29
20120906093300 55.04 87.81 87.81 84.28
20120906093400 55.04 87.82 87.82 84.28
20120906093900 55.03 87.81 87.81 84.27
20120906094000 55.02 87.81 87.81 84.28
20120906094100 55.02 87.81 87.81 84.29
20120906094200 55.02 87.81 87.81 84.29
20120906094300 55.01 87.82 87.82 84.28
20120906094700 55.00 87.81 87.81 84.28
20120906094800 55.00 87.82 87.82 84.29
Thanks your help. Thanh Cao

回答(1 个)

Akira Agata
Akira Agata 2018-4-16

Like this?

fid = fopen('YourData.txt','r');
C = textscan(fid,'%s%f%f%f%f%*4s','Delimiter',';');
fclose(fid);
A = string(C{1});
B = cell2mat(C(2:end));

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by