Problem to show image in GUI automatically (without press push button)

2 views (last 30 days)
Hi,
I made a very simple GUI (Robot_GUI) with guide command in MATLAB. I put a push button (to run sorgum.m) and also two axis (to show the output images of the m-file) in the GUI. I need to run the m-file (sorgum) and also show the output images which are saved in MATLAB directory (leaf_reflectance1.tif and leaf_reflectance2.tif) with GUI automatically (without press push button). In the GUI, by pressing the push button, I can run the m-file. But for showing the images, it gives me an error. The GUI and also the error are in below:
//GUI
function varargout = Robot_GUI(varargin)
% ROBOT_GUI MATLAB code for Robot_GUI.fig
% ROBOT_GUI, by itself, creates a new ROBOT_GUI or raises the existing
% singleton*.
%
% H = ROBOT_GUI returns the handle to a new ROBOT_GUI or the handle to
% the existing singleton*.
%
% ROBOT_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ROBOT_GUI.M with the given input arguments.
%
% ROBOT_GUI('Property','Value',...) creates a new ROBOT_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Robot_GUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Robot_GUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Robot_GUI
% Last Modified by GUIDE v2.5 06-Apr-2017 15:48:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Robot_GUI_OpeningFcn, ...
'gui_OutputFcn', @Robot_GUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Robot_GUI is made visible.
function Robot_GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Robot_GUI (see VARARGIN)
% Choose default command line output for Robot_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Robot_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Robot_GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sorgum
pic1=imread ('leaf_reflectance1.tif');
axes(handles.axes1);
image (pic1);
axis off;
guidata(hObject,handles)
pic2=imread ('leaf_reflectance2.tif');
axes(handles.axes2);
image (pic2);
axis off;
guidata(hObject,handles)
//Error
Error using imread (line 349) File "leaf_reflectance1.tif" does not exist.
Error in Robot_GUI>pushbutton1_Callback (line 83) pic1=imread ('leaf_reflectance1.tif');
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in Robot_GUI (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Robot_GUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating UIControl Callback
How can I fix this issue? I appreciate in advance.
Thank you,
Abbas
  1 Comment
Adam
Adam on 7 Apr 2017
Did you actually read the error? It's hard for the forum to help you with an error that says the file you are trying to open does not exist. The error itself is very clear so what extra information is needed?

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 7 Apr 2017
Here's how to fix it. Instead of putting "leaf_reflectance1.tif" into the MATLAB directory (wherever/whatever that is), put it into the folder where your Robot_GUI.m file lives. Or even better, specify the folder, either in code or by calling uigetdir() to have the user locate it, and specify the full file name with fullfile().
startingFolder = mfilename; % Whatever...
folder = uigetdir(startingFolder);
fullFileName = fullfile(folder, 'leaf_reflectance1.tif');
if exist(fullFileName, 'file')
% File exists. Read it in.
pic = imread(fullFileName);
else
% File does not exist. Warn User
warningMessage = sprintf('Warning: image not found:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end

Paramesh
Paramesh on 7 Apr 2017
This error shows "leaf_reflectance1.tif" does not stored in the directory,check image is there or not?

chan
chan on 18 Jul 2019
You just add command above gui_Singleton = 1; Like that, A = imread('filename'); imshow(A) Make sure that file image must be in the same folder of m and fig file.

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!