App designer - issue with app freezing after performing the first task
Show older comments
Hi folks,
I have an app that's meant to display an image from a folder with plots on them in the shape of a crosshair.
Currently, it is freezing after loading the image folder and displaying the first image, and I'm uncertain as to why.
Could you please point me in the direction of where my code is incorrect?
checkMatrix is a matrix of 9 checkbox values.
The issue appears to be in the "DisplayCrosshairs" section. The very first line causes the programme to sieze up and become completely un-interactable.
Thanks
methods (Access = private)
function ROI(app, index1, index2)
hold on
plot(index1, index2, app.innerMarkerStyle, 'MarkerSize', app.CrosshairThickness.Value, 'Color', app.crossHairColour);
plot(index1, index2, app.outerMarkerStyle, 'MarkerSize', app.CrosshairThickness.Value, 'Color', app.crossHairColour);
end
function DisplayCrosshairs(app)
[rows, columns] = size(app.Image, [1, 2]);
quarterHeight = rows*0.25;
halfHeight = rows*0.5;
threeQuarterHeight = rows*0.75;
quarterWidth = columns*0.25;
halfWidth = columns*0.5;
threeQuarterWidth = columns*0.75;
checkMatrix = [app.TopLeft.Value app.TopMiddle.Value app.TopRight.Value app.CentreLeft.Value app.CentreMiddle.Value app.CentreRight.Value app.BottomLeft.Value app.BottomMiddle.Value app.BottomRight.Value];
app.numSamplesPerImage = sum(checkMatrix, 1:9);
if checkMatrix(1) == 1
ROI(app, quarterHeight, quarterWidth);
elseif checkMatrix(2) == 1
ROI(app, quarterHeight, halfWidth);
elseif checkMatrix(3) == 1
ROI(app, quarterHeight, threeQuarterWidth);
elseif checkMatrix(4) == 1
ROI(app, halfHeight, quarterWidth);
elseif checkMatrix(5) == 1
ROI(app, halfHeight, halfWidth);
elseif checkMatrix(6) == 1
ROI(app, halfHeight, threeQuarterWidth);
elseif checkMatrix(7) == 1
ROI(app, threeQuarterHeight, quarterWidth);
elseif checkMatrix(8) == 1
ROI(app, threeQuarterHeight, halfWidth);
elseif checkMatrix(9) == 1
ROI(app, threeQuarterHeight, threeQuarterWidth);
else
msgbox('Please select a quadrant for the crosshair to be displayed in');
end
end
function DisplayImage(app)
app.currentImage = fullfile(app.imageFolder(app.imageNumber).folder, app.imageFolder(app.imageNumber).name);
app.Image.Position = [0 0 app.UIFigure.Position(3:4)];
app.img = imread(app.currentImage);
app.uiImage = uiimage(app.UIFigure, 'imagesource', app.currentImage, "Position", app.Image.Position);
DisplayCrosshairs(app);
end
function LoadFolder(app)
app.imageFolder = dir(fullfile(app.filePath, '*.jpg'));
app.fileNumber = numel(app.imageFolder);
end
function CheckEndOfList(app)
if app.imageNumber < app.fileNumber
app.imageNumber = app.imageNumber +1;
else
msgbox('You have reached the last image');
end
end
end
function startupFcn(app)
pause(1);
app.UIFigure.WindowState = 'maximized';
app.Counts = [zeros(8, 1)];
app.Percentages = [zeros(8, 1)];
app.CokeTable.Data = [app.Counts, app.Percentages];
app.CokeTable.Position = [73 -1 168 265];
app.tallyOrder = [1];
app.innerMarkerStyle = '+';
app.outerMarkerStyle = 'o';
app.crossHairColour = 'w';
app.ImageNumber.Value = app.imageNumber;
app.TotalNumber.Value = app.fileNumber;
end
function AddImageFolderMenuSelected(app, event)
app.filePath = uigetdir(pwd);
addpath(app.filePath);
LoadFolder(app);
DisplayImage(app);
CheckEndOfList(app);
app.ImageNumber.Value = app.imageNumber;
app.TotalNumber.Value = app.fileNumber;
end
5 Comments
Adam Danz
on 24 Mar 2021
Place a break point at the top of the AddImageFolderMenuSelected function (assuming this is where the problem is) and step through the function in debug mode. Then tell us which line leads to freezing.
Teshan Rezel
on 24 Mar 2021
Adam Danz
on 24 Mar 2021
So this is the line that causes the freeze?
[rows, columns] = size(app.Image, [1, 2]);
That's surprising. What's app.Image?
class(app.Image)
Does it freeze when you execute this line below? If not, what's the output?
size(app.Image)
Teshan Rezel
on 25 Mar 2021
Adam Danz
on 25 Mar 2021
Replied below in Jan's answer.
Accepted Answer
More Answers (1)
Jan
on 24 Mar 2021
Appending folders to Matlab's PATH can lead to unexpected side effects, if the folders contain Matlab files. So avoid addpath but use absolute path names using app.filePath and fullfile() - as you do already. So remove this line:
addpath(app.filePath);
But this is most likely not the source of the problem.
Adam's suggestion to use the debugger to step through the code line by line is the way to locate the line, which causes the troubles.
6 Comments
Teshan Rezel
on 24 Mar 2021
Which is the first line of DisplayCrosshairs? This one:
[rows, columns] = size(app.Image, [1, 2]);
? I do not find in your posted code, where app.Image is defined. Therefore I guess, that Matlab tries to show a corresponding error message, but fails for any reason. Or does the command window show an error? What happens, if you use the debugger:
dbstop if error
Does it stop in this line?
Teshan Rezel
on 25 Mar 2021
Jan
on 25 Mar 2021
This sounds like app.imageNumber is not an valid index. So check, how it is defined. I do not see the definition in your posted code.
This is the first line of DisplayImage(app), not the DisplayCrosshairs function as you indicated earlier. It's a minor mistake not to worry about but it did result in time troubleshooting the wrong parts of the code.
More importantly, now we know that the app isn't freezing. It's not just stopping due to an error and an error message should have appeared in the command window and in app designer code view if the app was opened in app desginer. That's an important lesson - check the command window for error messages if a program stops unexpectedly.
I agree with Jan, app.imageNumber is neither a positive integer or logical value as the error message indicates. Maybe it's a valid number wrapped in a cell such as {1} or perhaps it's empty or NaN.
Teshan Rezel
on 26 Mar 2021
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!