Reading in text file using textscan.
138 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Andrew Czeizler
am 8 Mär. 2019
Kommentiert: Andrew Czeizler
am 11 Mär. 2019
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 -
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207461/image.png)
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 Kommentare
Akzeptierte Antwort
Stephen23
am 10 Mär. 2019
Bearbeitet: Stephen23
am 11 Mär. 2019
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}.';
Weitere Antworten (1)
KSSV
am 8 Mär. 2019
Read about readtable.
T = readtable(myfile) ;
1 Kommentar
Stephen23
am 8 Mär. 2019
Andrew Czeizler's "Answer" moved here:
Thank you for your help. The question has requested that I use textscan.
Many thanks,
Best,
Andrew
Siehe auch
Kategorien
Mehr zu Data Import and Export finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!