MATLAB Answers

0

Why I get extra columns with irrelevant results?

Asked by Stelios Fanourakis on 24 Feb 2019
Latest activity Commented on by Stelios Fanourakis on 25 Feb 2019
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by Matt J
on 24 Feb 2019
 Accepted Answer

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 Comments

Yes it is the dist, but at the moment, i have changed the numbers of col from previous image and the number of rows are also different from previous image.
When the essential values are changed and Run the script, why it should remember previous values?
I don't think to have faced the same issue before
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.
Yeah, thanks. Most probably this was the case.

Sign in to comment.