How to show an image once starting GUI

3 ビュー (過去 30 日間)
ws
ws 2011 年 11 月 18 日
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');

採用された回答

Image Analyst
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

その他の回答 (1 件)

ws
ws 2011 年 11 月 20 日
Hi guys
I'm not sure the method used is correct, but I have my image to appear on my main uipanel:
function axes_background_CreateFcn(hObject, eventdata, handles)
axes(handles.axes21)
imshow('main_bg.jpg')
:)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by