0

I am trying to read data from this .txt:

obiekt.DEF


Timeplot
Column01: P abs h01 L1 [W]
Column02: P abs h01 L2 [W]
Column03: P abs h01 L3 [W]
Column04: P abs h01 Sum [W]

Time                         Column01    Column02    Column03    Column04
11.03.2004 09:17:02              23500       19812       21529    64,84e+3
11.03.2004 09:17:05              23316       19789       21519    64,62e+3
11.03.2004 09:17:08              23207       19759       21392    64,36e+3

I only need data from column: 01,02,03. Some data have ',' instead '.'. How to change it? I have many file like this. I tried this function, but it write all data to one variable.

b=textread('test.txt','%s','delimiter',' ','whitespace',' ');

1 Answer 1

1

You could use textscan:

filename='myfile.txt';
fid=fopen(filename,'r');
data=textscan(fid,'%*s%*s%s%s%s%*s','HeaderLines',10,'CollectOutput',1);
fclose(fid);
data=strrep(data{1},',','.');
data=cellfun(@str2num, data);

Setting HeaderLines sets the first 10 lines to be ignored. Setting CollectOutput groups items of the same type into a cell array (so we get 3 columns of strings). The formatspec '%*s%*s%s%s%s%*s' ignores the date, time and Column04 and converts Column01-03 to strings. Then strrep replaces the commas with periods. cellfun calls str2num on each cell and converts the string to a number.

Sign up to request clarification or add additional context in comments.

4 Comments

Works perfectly, but when I open another file, I got error
I can't access your link at the moment (internet restrictions), could you update your question with the details of the error and the file that produces it?
I just checked out your image and updated my answer, it should be the right idea, but I can't test it until the morning. I'll update it if it isn't right.
I've updated the answer after testing it this morning. Hopefully that's got it now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.