How to show an image once starting GUI
조회 수: 3 (최근 30일)
이전 댓글 표시
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');
댓글 수: 0
채택된 답변
Image Analyst
2011년 11월 19일
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
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!