MATLAB: How to read out a textfile from a specific line

delete text out of textfilereadouttexread

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 🙂

Best Answer

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: