Why will my data repeated itself when writing to Excel?

조회 수: 6 (최근 30일)
Ziming Zhong
Ziming Zhong 2018년 2월 2일
댓글: dpb 2018년 2월 2일
Hello!
I found a problem when I tried my codes to write out the data of connected components. In my picture, there are only 91 objects were chose, but in my read-out Excel file I got 98 objects. I want to see why, and so I compared every line of the data and find out some repetitions in my Excel file! As I show in the picture, line 93,94,95 are repetition of line 90,91,and 92, then another repetition of the three lines and line 99 is the repletion of line 98. I don't know why this happens, because the data in the workspace are correct without any repetition. The repetition happens nearly in all of my file, except the pattern of repetition is different, as I show in the second picture from another file.
I guess it could be that my script for writing out the data is not robust. But I can't improve it. Can somebody help? Thanks!
My script:
KeeperComponentsMeasurement=regionprops(keeperComponents,'Centroid','PixelIdxList','Area'); %find the components and measure them
p=[];
q={};
for k = 1:numel(KeeperComponentsMeasurement)
oneIdxlist = KeeperComponentsMeasurement(k).PixelIdxList; %calculate the luminance
oneGray = d(oneIdxlist); %calculate the luminance
meanGray = uint8(mean(oneGray)); %calculate the luminance
p=[p meanGray]; %create array for luminance
q=[q k]; % create a array for the index of components
end
% write out the data of connected components
pp=num2cell(transpose(p));
qq=num2cell(transpose(q));
KeeperComponentsMeasurement2 = rmfield(KeeperComponentsMeasurement,'PixelIdxList');
[KeeperComponentsMeasurement2(:).MeanGrey]=pp{:}; %add a column
[KeeperComponentsMeasurement2(:).ColonyNum]=qq{:};
worldArea=num2cell([KeeperComponentsMeasurement.Area].*caliPara.^2); %turn the pixel into cm^2
[KeeperComponentsMeasurement2(:).worldAreacm]=worldArea{:};
xlsfilename=[name '-CC' '.xls'];
writetable(struct2table(KeeperComponentsMeasurement2),fullfile(DataFolder, xlsfilename));
  댓글 수: 1
dpb
dpb 2018년 2월 2일
Not possible to diagnose without the data, sorry...undoubtedly there is repeated data somewhere; probably owing to the struct2table transition needing to duplicate fields for regularity in the transition from a structure array to a table.
It would seem cleaner to build a table and/or array instead directly.

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

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2018년 2월 2일
편집: Fangjun Jiang 2018년 2월 2일
I know xlswrite() has this type of problem. If the Excel file already exists, it writes the data to the existing sheet without wiping out the existing data first. Your problem might be similar. The data is there because it was the result of your previous run. Run the code below to see my point.
So delete the Excel file before you write to avoid this problem.
xlswrite('test',magic(5));
xlswrite('test',rand(3));
winopen('test.xls');
  댓글 수: 11
dpb
dpb 2018년 2월 2일
Start over by first physically deleting all .xls files you've created to date by this process to ensure what you're looking at from here on is fresh.
Then, I'd recommend trimming the loop down to just a few and work through checking that the result is correct and that your naming scheme for each file generates a unique name.
In this way, there's no way the above issue of overwriting the data of a previous case with a smaller subset or the like can be the issue.
If you then have duplicate lines there are only two choices for why --
  1. your data actually is being duplicated somehow a la my initial comment, or
  2. you've found a bug.
You are correct the xlswrite is implemented for Excel files only for Windows systems; writetable uses builtin or licensed methods or whatever it takes to work on Mac.
It's always best in cases such as this to just be doubly-sure talking same intent/operations even if think there can't be any misunderstandings; the code snippet shown won't write multiple files so the "how" of that wasn't defined unequivocally.
dpb
dpb 2018년 2월 2일
"Sorry that I can not list all my codes here and attach the picture for test analysis, as the whole analysis code have more than 200 lines. But I guess those picture analyzing codes are fine. It’s only the data export part cause this repeatition problem."
There's nothing preventing attaching a .mat file of an image and the code file that will reproduce the symptom on your machine.
If you can build such a sample case; that could help immeasurably as having something to actually work on beats guessing at what might be going on all to pieces.
It might be a useful exercise to just try to do the above anyway; if you can't reproduce it for a single case, that would indicate the issue is something about the previous state/file and that alone may be enough for you to find the problem yourself.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by