필터 지우기
필터 지우기

Identify first instance of something in a .dat file

조회 수: 2 (최근 30일)
Victoria Lawlor
Victoria Lawlor 2015년 10월 22일
댓글: TastyPastry 2015년 10월 27일
I have a data file where one tab contains the picture that was presented on the screen another tab has the timestamp. Below is a really short version of what it looks like.
Dog 1ms
Dog 2ms
Dog 3ms
Cat 4ms
Cat 5ms
Cat 6ms
Dog 7ms
Dog 8ms
Dog 9ms
Cat 10ms
Cat 11ms
Cat 12ms
I need to get the rows with the first instance in the grouping (?) of that picture with the timestamp. Below is what I want to have:
Dog 1ms
Cat 4ms
Dog 7ms
Cat 10ms
I'm not sure how to go about doing this; any help would be appreciated!

채택된 답변

TastyPastry
TastyPastry 2015년 10월 22일
fid = fopen('test.txt');
data = textscan(fid,'%s %s');
data = [data{1} data{2}];
out = data(1,:);
for i=2:numel(data(:,1))
if ~strcmpi(data{i-1,1},data{i,1})
out = [out;data(i,:)];
end
end
  댓글 수: 4
Victoria Lawlor
Victoria Lawlor 2015년 10월 23일
hmm thanks for replying, I'm getting an error that the index exceeds matrix dimensions. I also kind of oversimplified it when I posted the questions, there are actually four columns that I have and would need be displayed but I thought if I figured out how to do it with two I could figure out how to do it with 4, could that be the issue?
TastyPastry
TastyPastry 2015년 10월 27일
Did you run the test.m with test.txt? I just the script and it's working fine. I doubt it's an issue with more than 1 column, since the code limits the data it pulls to the first two.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by