Read textfile seperated by ':' and put it in a table
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
TastyPastry
il 19 Ott 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 Commenti
Guillaume
il 20 Ott 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);
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!