Why I get extra columns with irrelevant results?

2 次查看(过去 30 天)
Hi
I use those lines to get the number of pixels among several rows and columns
for col = 390 : 466
h = imdistline(gca,[427 427],[215 370]);
dist(col) = getDistance(h)
end
Then I use this line to export them in excel
data = [dist(:)];
t = table(data);
writetable(t,'YourFile555.xls','Sheet',1,'Range','AD1:AD520')
Other times, when the Range exceeds the pixel values of the image I am supposed to get from dist, then the exceeding cells of the excel would get 0 value.
In my case, I get the correct values up until the 466 column as I have defined in the for loop and then it fills with irrelevant values, the rest of the columns until 520 as I have declared in the Range. Why is that?
155 is the correct value of the image I am using, the rest 83 that you see are values of a previous image. Not the one I am currently use. Why Matlab gets confused??
Columns 457 through 475
155 155 155 155 155 155 155 155 155 155 83 83 83 83 83 83 83 83 83
Columns 476 through 494
83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83 83
Columns 495 through 508
83 83 83 83 83 83 83 83 83 83 83 83 83 83
I don;t have any other line in the code that refers to the previous image. Just the ones I write here.
Any possible explanation please?

采纳的回答

Matt J
Matt J 2019-2-24
My guess is that you are writing into the same file as the previous image, but with a shorter list of values. The preceding values are still there because you did not clear them and the list of new values is too short to overwrite them all.
  5 个评论
Matt J
Matt J 2019-2-24
编辑:Matt J 2019-2-25
When the essential values are changed and Run the script, why it should remember previous values?
Because unlike mfunction files, scripts don't have their own workspace. Any variables generated when you run a script reside in the workspace where the script is run, and remain there until you clear them. So, when you run the script a second time, assignment commands like,
dist(col) = getDistance(h)
are just modifying the contents of the dist variable previously created. They aren't creating it from scratch.
Just put in a "clear" command as the first line of the script and it should fix that problem.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by