Randomly Appearing Zeros in Data Output

조회 수: 13 (최근 30일)
Mary
Mary 2014년 3월 31일
댓글: Mary 2014년 4월 1일
Hello!
I'm still working on some code I have asked about in previous questions. All seems to be going well except in my output I keep getting zeros unexpectedly and I can't seem to figure out why
The code opens data, uses accumarray to place the data into vectors and then writes the output of the vectors into a matrix. For some reason I seem to get random rows of zero in this matrix - and I cant seem to find out why
The code is below:
% Cycle through files rownum = 1;
for f = 1: numberOfFiles resFileName = fullfile(res_files,baseFileNames(f).name) Name = baseFileNames(f).name rawData = importdata(resFileName,' ');
timeData = rawData.data(:,1);
ampData = rawData.data(:,2);
PIDget = Name(3:5)
PID = str2num(PIDget)
Task1.PID{f} = PID;
binnum = 1 + floor(timeData(:) / 39);
Task1.Amp(f).trial = accumarray( binnum, ampData(:), [], @(V) {V})
Task1.Time(f).trial = accumarray( binnum, timeData(:), [], @(V) {V})
numtrial = numel(Task1.Amp(f).trial)
% for trialnum = 1:numel(Task1.Amp(f).trial)
binnum(end)
for trialnum = 1:binnum(end)
for numbers = 1:numel(Task1.Amp(f).trial{trialnum})
timedata = Task1.Time(f).trial{trialnum}(numbers);
ampdata = Task1.Amp(f).trial{trialnum}(numbers);
DATA_raw(rownum,1) = PID;
DATA_raw(rownum,2) = trialnum;
DATA_raw(rownum,3) = timedata;
DATA_raw(rownum,4) = ampdata;
rownum = rownum + numbers;
end
end
end
The output data file DATA_Raw looks like this...
2 3 114.080000000000 0.218000000000000
0 0 0 0
0 0 0 0
0 0 0 0
2 4 120.260000000000 0.0350000000000000
Where are these zero's coming from???
Thanks, ML
  댓글 수: 2
dpb
dpb 2014년 3월 31일
Where is there an "output data file" or do you just mean the array in memory?
W/o any data to be able to see for certain it's not possible to say for absolute certain, but a reasonable conjecture is that your accumarray indices aren't what you think they are.
Best recommendation I can give is to use the debugger and step thru your code watching what happens as you process the data. I'll wager you'll find an "ah-ha!" moment when all becomes clear...now that may engender some other questions on how to best handle what you find, but I'll bet you find out the "why". :)
Mary
Mary 2014년 4월 1일
Thank you I'll give debug a try!

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

채택된 답변

Joseph Cheng
Joseph Cheng 2014년 3월 31일
편집: Joseph Cheng 2014년 3월 31일
I am suspecting that it is coming from your rownum = ronum+numbers.
From your snippit rownum starts at 1 and then gets incremented by numbers. It's basically defining a row in DATA_raw and then defining another row at (rownum+numbers) further down for the next iteration. since numbers isn't 1 you won't get sequential rows of data. When that happens MATLAB will zero fill the empty spaces.
simple display of whats happening
x=rand(1,4)
x(5,:)=rand(1,4)
I see where this may have worked if you get PID, trailnum, etc as size numbers*1 matrix where you'd need to increment the indexing by numbers and not just 1. However you'll additionally need to DATA_raw(rownum:rownum+numbers-1,1) which will get a numbers*1 matrix to go into that space.
  댓글 수: 1
Mary
Mary 2014년 4월 1일
Thank you! The example you gave was really helpful - I should have realized that sooner.
Hopefully fixing the way numbers is called will clean things up!
Thank you for such a quick and thoughtful response!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by