Hi All, I need to read a text file into four row vectors named bottleID, date, ph and pressure using textscan.
The file has the following format -
I have made an attempt but I am finding the function textscan quite difficult to use.
My attempt is below -
fileID = fopen('hots_data.txt','r');
format_string = '%n %[{dd-MMM-yyyy}D]%q %d %d';
C = textscan(fileID, format_string, 'delimiter', ' ', 'whitespace', ' ');
fclose(fileID);
Any help would be very much appreciated.
Many thanks,
Best,
Andrew

댓글 수: 6

KSSV
KSSV 2019년 3월 8일
YOu shoul dupload a text file or copy and paste a snippet of the file. Attaching image is of no help.
Stephen23
Stephen23 2019년 3월 8일
Andrew Czeizler's "Answer" mvoed here:
Apologies file is below -
370100119 08-Jun-1992 8.102000 9.600000
370100118 08-Jun-1992 8.097000 29.500000
370100104 08-Jun-1992 7.601000 406.400000
370100103 08-Jun-1992 7.448000 505.400000
370100102 08-Jun-1992 7.369000 754.500000
370100101 08-Jun-1992 7.401000 1029.000000
380100117 03-Jul-1992 8.105000 9.100000
380100116 03-Jul-1992 8.103000 29.000000
380100105 03-Jul-1992 7.835000 230.800000
380100104 03-Jul-1992 7.766000 254.500000
380100103 03-Jul-1992 7.705000 305.600000
380100102 03-Jul-1992 7.609000 406.500000
380100101 03-Jul-1992 7.483000 490.300000
390100118 03-Aug-1992 8.112000 28.400000
390100117 03-Aug-1992 8.110000 49.200000
Stephen23
Stephen23 2019년 3월 8일
@Andrew Czeizler: please upload a sample file by clicking the paperclip button.
Andrew Czeizler
Andrew Czeizler 2019년 3월 8일
Thank you! Give me a sec :).....
Best,
Andrew
Andrew Czeizler
Andrew Czeizler 2019년 3월 8일
I have uploaded the file. Tried a loop technique with no success ......
Totally lost on this one.
Best,
Andrew
Andrew Czeizler
Andrew Czeizler 2019년 3월 10일
편집: Andrew Czeizler 2019년 3월 10일
Hi All,
I was able to get textscan to work, but I cant read it into rows?
Is there a method that I am missing?
Best.
Andrew
fileID = fopen('testdata1.txt','r');
C = textscan(fileID, '%d %{dd-MMM-yyyy}D %f %f', 'Delimiter', ' ');
fclose(fileID);

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2019년 3월 10일
편집: Stephen23 2019년 3월 11일

0 개 추천

textscan imports file data into one cell array, the contents of which are one or more arrays (numeric, cell, datetime, etc), where their number of rows depends on the rows imported from the file and their columns depends on the format specifier and options that you used.
textscan does not import into separate variables, or transpose imported data.
"I need to read a text file into four row vectors named bottleID, date, ph and pressure using textscan."
[fid,msg] = fopen('testdata1.txt','rt');
assert(fid>=3,msg)
C = textscan(fid, '%d%{dd-MMM-yyyy}D%f%f', 'Delimiter',' ');
fclose(fid);
bottleID = C{1}.';
date = C{2}.';
ph = C{3}.';
pressure = C{4}.';

댓글 수: 1

Andrew Czeizler
Andrew Czeizler 2019년 3월 11일
Thank you Stephen! Legend!
I understand the concepts.
Best,
Andrew

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2019년 3월 8일

0 개 추천

Read about readtable.
T = readtable(myfile) ;

댓글 수: 1

Stephen23
Stephen23 2019년 3월 8일
Andrew Czeizler's "Answer" moved here:
Thank you for your help. The question has requested that I use textscan.
Many thanks,
Best,
Andrew

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2019년 3월 8일

댓글:

2019년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by