How to read out a textfile from a specific line

Hello Community,
I have a probably rather easy problem (at least for you guys). I have a textfile (attached) that is structured in lines and collums. And I want two specific collumns out of that file (Plasma_voltage_actual and Plasma_current_actual). Problem is that the first part of that text file has a different structure (just lines). So first I need to delete all the unnecessary stuff before the collumns start (actually no idea how to do that). That would be everything up to the line with which says "DATA RECORDINGS". What I want in the end is a Matrix with the headlines (Time Pressure Pres_set ...) and then the values for each collumn. Can you help me with that? The goal in the end is to take those two collumns and calculate the mean of them. But I guess I can (hopefully) figure that out on my own.
Thank you very much :)

 Accepted Answer

Stephen23
Stephen23 on 23 Jan 2019
Edited: Stephen23 on 23 Jan 2019
opt = {'Delimiter','\t', 'CollectOutput',true};
[fid,msg] = fopen('pG8.txt','rt');
assert(fid>=3,msg)
% Ignore lines until "DATA RECORDING":
str = '';
while ~strcmpi(str,'DATA RECORDING:') && ~feof(fid)
str = fgetl(fid);
end
% Read table header:
str = fgetl(fid);
hdr = regexp(str,'\t','split');
% Create format string:
fmt = repmat({'%*s'},1,numel(hdr));
idx = ismember(hdr,{'Plasma_voltage_actual(V)','Plasma_current_actual(mA)'})
fmt(idx) = {'%f'};
% Read table:
tmp = textscan(fid,[fmt{:}],opt{:});
fclose(fid);
out = tmp{1}
Which gives:
>> size(out) % nice big matrix of data:
ans =
3208 2
>> out(1:10,:) % not so interesting:
ans =
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
Lets try plotting it
X = 1:size(out,1);
plotyy(X,out(:,1),X,out(:,2))
legend(hdr(idx),'interpreter','none')
Giving:

7 Comments

Love you man! Thanks a lot
Sorry if this is something that should be easily done, but how should I modify this code if I want the "out"-matrix to contain not only two columns from the file but all of them? (Asking because I have a similar problem but want to keep and analyze all of the columns from the file.)
@Heidi Mäkitalo: simply define the format string to read all columns, e.g.
fmt = repmat('%f',1,numel(hdr));
You will have to select an appropriate column format specifier for your data.
In the same way as you ignored lines till DATA RECORDING is it possible to ignore lines inbetween 2 strings?
For eg.
DATA1
...................
......................
DATA2
..................
..................
Is it possible to ignore contents inbetween DATA1 and DATA2 then read contents under DATA2
@MANISH R Please ask follow-up questions in your own thread. You posted a question, but you have not responded with any comment.
Then you didn't explain it clearly enough. The way I read your comment it is not a separate question. The way I read your comment, this will be a trivial step once you implement the answer you already received in your own question. You simply ignore the first element of the cell vector.
I'm just saying the solution provided in that thread will also work for the problem you describe here. You're free to ignore the advice if you wish.
You want to read text, the other answer shows you how to read text. I don't understand why you are creating a new problem when you have a solution that can trivially be adapted.

Sign in to comment.

More Answers (1)

You might try READTABLE with import options:
>> opts = detectImportOptions(filename)
% Check that the number of header lines looks right
>> opts.SelectedVariableNames = {... the names you want to read ...}
>> T = readtable(filename,opts)

Categories

Asked:

on 23 Jan 2019

Commented:

Rik
on 22 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!