Question

I am learning gui at the moment and working on implementing simple basic processing functions. I have successfully written and done everything in the MATLAB using gui but stuck at only one small (basic) thing. Passing an argument as input.

Right now my code is taking a "hardcoded" image within the generated m file.

function varargout = testfinal(varargin)
% TESTFINAL MATLAB code for testfinal.fig
%      TESTFINAL, by itself, creates a new TESTFINAL or raises the existing
%      singleton*.
%
%      H = TESTFINAL returns the handle to a new TESTFINAL or the handle to
%      the existing singleton*.
%
%      TESTFINAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TESTFINAL.M with the given input arguments.
%
% ........

e.g Im=Imread('myimage.jpg'); in the opening function as shown below:

function testfinal_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 testfinal (see VARARGIN)
% Choose default command line output for testgui

handles.output = hObject;
Img=imread('Myimage.jpg');
.....

What I am looking to do now is to be able to pass the image filename through command window e.g in the command line I can write

testfinal('Myimage.jpg');

and this will show the image on the axes on GUI (which is already done using the hard coded method) and be able to do the rest as before.

Any help?? I can't seem to figure out how to do it with GUI.

Was it helpful?

Solution 2

You can pass the image path to the GUI through the Matlab command line, and use that path in the _OpeningFcn of your Guide GUI. In the example below I simply display the input string in a text box. Do take into account absence of any inputs as well.

% --- Executes just before GuiWithInput is made visible.
function GuiWithInput_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 GuiWithInput (see VARARGIN)

% Choose default command line output for GuiWithInput
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

if(numel(varargin))
    TextToDisplay = varargin{1};
else
    TextToDisplay = 'Dummy String to be displayed!';
end

set( handles.edit1, 'String', TextToDisplay);

The varargin variable in the _OpeningFcn function, contains the command line inputs supplied to the GUI by the user. It is of type cell array, with each cell containing inputs supplied.

Assuming the call to your GUI is,

GUI_Name( 'input1', 2, [ 2; 4; 6])

you can access those inputs in the _OpeningFcn using:

varargin{1}       % contains a string 'input1'
varargin{2}       % contains a double with value 2
varargin{3}       % contains a numeric column array with value [ 2; 4; 6]

OTHER TIPS

As you're writing a GUI, you probably would be prefer a more GUI-like solution rather than the conventional possibilities input and inputdlg: use uigetfile.

filetype = '*.jpg';
description =  'myImages';
dialogtitle = 'Load my images';
defaultpath = ['c:\...];         %which is opened by dialog start

[filename, pathname] = uigetfile({filetype,description},dialogtitle,defaultpath);
Img = imread( [pathname filename] );

or with

testfinal( [pathname filename] );

or just

testfinal( filename );

if you stay in your workspace anyway.

For simpler or more complex examples see the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top