MATLAB: How to show an image once starting GUI

axesmatlab guisplash screen

Hi all
I set code below on axes_background_CreateFcn, it works fine, but sometimes I re-run the GUI, axes tag will become blank and unable to show my image.
function axes_background_CreateFcn(hObject, eventdata, handles)
axes(hObject);
imshow('main_bg.jpg');

Best Answer

I think it's best if you put the imshow() in the OpeningFcn() rather than the create function. The OpeningFcn is the function where you're supposed to put all your startup code - see the comments for it.
Other problems with your code are that
1. You failed to use the full file name by using fullfile() to prepend the folder. See the FAQ ( http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function. )
2. You failed to check for existence of the file with the exist(fullFileName, 'file') function.
3. You failed to use try/catch,
try
fullFuleName = fullfile(folder, 'main_bg.jpg');
if exist(fullFileName, 'file)
imshow(fullFilename);
else
errorMessage = sprintf('Error: file does not exist:\n%s', fullFileName);
uiwait(warndlg(errorMessage));
end
catch ME
% Some general error.
errorMessage = sprintf('Error in function axes_background_CreateFcn.\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
You can improve the robustness of your code if you read and follow the guidelines here: http://www.mathworks.com/matlabcentral/answers/1148-how-to-learn-matlab
Related Question