Read textfile seperated by ':' and put it in a table
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
TastyPastry
2015-10-19
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 个评论
Guillaume
2015-10-20
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);
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!