Display image if file is present, else display blank image.

3 views (last 30 days)
Good day! I would like to ask on how to display an image if the file exists and if the file does not exist, it simply displays blank image in MATLAB GUI.
I am currently working on MATLAB GUI wherein it should display specific images. For an example, file1 displays on axis1 and file2 displays on axis2. That already works but if file2 is not present, the GUI gets an error because file2 does not exist yet.
So far, I would like the axis to display a blank image, let's say defaultfile0 whenever the file doesn't exist yet. Can anyone suggest on how to do this one? Thank you so much.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Oct 2015
filename = sprintf('filexyz%03d.jpg', round(1000*rand())); %some file the might or might not exist
if exist(filename, 'file')
MyImage = imread(filename);
elseif exist(defaultfile0, 'file');
MyImage = imread(defaultfile0);
else
MyImage = zeros(128,128,3); %_something_
end
image(MyImage);

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!