Read text file data

1 view (last 30 days)
Thanh Cao
Thanh Cao on 16 Apr 2018
Answered: Akira Agata on 16 Apr 2018
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

Answers (1)

Akira Agata
Akira Agata on 16 Apr 2018

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));

Community Treasure Hunt

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

Start Hunting!