Read textfile seperated by ':' and put it in a table
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
pdefesche
am 19 Okt. 2015
Beantwortet: pdefesche
am 21 Okt. 2015
Hi,
I have some data which i would like to use in Matlab. the following file is an example:
% test.txt
!Drawing Name: 1234567 Drawing File: 1234567_0
%Section 1: Header info
1234567:00:19/10/2015::::AB::::CD::::KL
%Section 2: data
1:2:3:4:5:6:7:8::::::::::::::::::::::::::900
%End of File Marker
I want to place this data into a table which makes a new column if ':' and makes a new row if 'CR LF' (enter). The data can always change in length, so section 1 can have 10 delimiters(':') and section 2 can have for example 30 delimiters(':'),
Can someone point me in the right direction or give me some example code? I've tried some functions, but without succes. So far the only function working for me is:
% code
h = fileread('test.txt');
Thanks in advance.
Regards,
pdefesche
0 Kommentare
Akzeptierte Antwort
TastyPastry
am 19 Okt. 2015
Use strsplit() with the delimiter ':'. It doesn't care how many colons are in a row. All the data will be put into a cell array of strings, so you'll need to modify the data type using str2double() if you want to use it for calculations.
3 Kommentare
Guillaume
am 20 Okt. 2015
fileread preserves the line returns when it reads a text file. What it does not do it line ending normalisation, so depending on the source of the file, the line ending is either going to be '\r\n' or '\n'. Nonetheless, you can still also use strsplit t separate the lines. You need to do that before the colon separation:
lines = strsplit(fileread('somefile.txt'), {'\r\n', '\n'});
%note the order of the split strings needs to be in that order
%to make sure '\r\n' takes precedence.
splitlines = cellfun(@(l) strsplit(l, ':'), lines, 'UniformOutput', false);
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Text Files 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!