How to read only some columns of a text file?

조회 수: 1 (최근 30일)
Samuele Bolotta
Samuele Bolotta 2020년 5월 11일
댓글: per isakson 2020년 5월 11일
I'd like to read out only columns from 3 to 7. However, this is not working. Can you help?
Thanks!
%Get events from text file
prompt_totalfiles = 'Enter number of files you want to input: '; %asks for number of files
total_files = input(prompt_totalfiles);
fileids = cell(total_files,1);
j = 1;
Baseline_adjustment = 0.1; %put in baseline photodetector value here (the readout with the led on but the patch cable in pitch black
wholedff = cell(1, 11);
for file = 1:total_files
[inputfile,path] = uigetfile('*.txt');
fileids{file} = fopen(fullfile(path, inputfile));
if fileids{file} == -1
error('Failed to open file "%s"', fullfile(path, inputfile));
end
b = textscan(fileids{file},'%*n %*n %n %n %n %n %n', 'delimiter', '/t');
num = b{3};
epochn = b{4};
state = b{5};
count = b{6};
duration = b{7};
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 5월 11일
'delimiter', '\t'
Also if columns 1 and 2 might not be numeric then use %*s for them.

댓글을 달려면 로그인하십시오.

답변 (1개)

per isakson
per isakson 2020년 5월 11일
편집: per isakson 2020년 5월 11일
The text file might have more than 7 coloums.
Replace
'%*n %*n %n %n %n %n %n'
by
'%*s%*s%n%n%n%n%n%*[^\n]'
%*[^\n]' skips to the end of line
The code doesn't include end to close the for-loop.
The result of the import might be overwritten for each iteration of the loop.
  댓글 수: 8
Samuele Bolotta
Samuele Bolotta 2020년 5월 11일
Ah no I actually found out the problem! I should use %2s instead of %s. Thanks!
per isakson
per isakson 2020년 5월 11일
" [...] I should use %2s instead of %s." No, the problem is rather with the delimiter. I assume there is no '\t' after the six coloumn.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by