In matlab gui, I have following code to load an image. I want to change it into user define function. is it possible and how?

1 view (last 30 days)
[FileName,PathName] = uigetfile('*.*','Select the MATLAB image file');
set(handles.edit1, 'String', FileName);
image_file = get(handles.edit1,'String');
if ~isempty(image_file)
im_original=imread(char(image_file));
set(handles.axes1,'HandleVisibility','ON');
set(handles.axes2,'HandleVisibility','OFF');
axes(handles.axes1);
image(im_original);
axis equal;
axis tight;
axis off;
set(handles.axes1,'HandleVisibility','OFF');
end;

Answers (2)

Andrew Reibold
Andrew Reibold on 15 Jul 2014
Edited: Andrew Reibold on 15 Jul 2014
Make a new function in the editor and I believe you can literally save this as loadmyimage.m (you can change the name to whatever) in whichever directory you want to call it from.
function loadmyimage
[FileName,PathName] = uigetfile('*.*','Select the MATLAB image file');
set(handles.edit1, 'String', FileName);
image_file = get(handles.edit1,'String');
if ~isempty(image_file)
im_original=imread(char(image_file));
set(handles.axes1,'HandleVisibility','ON');
set(handles.axes2,'HandleVisibility','OFF');
axes(handles.axes1);
image(im_original);
axis equal;
axis tight;
axis off;
set(handles.axes1,'HandleVisibility','OFF');
end;
end
Whenever you want the code to run, use the command 'loadmyimage'. (It needs to be saved in your current directory)
I'm assuming that all your variables (or in this case, what look like gui handles)are in the workspace. If they are not, add them as inputs after the function name.
You could also set a button to run the code in your gui (if thats what you are doing) if you want by putting your code in its respective callback function instead of making a user defined one.
Here is another reference for you

Image Analyst
Image Analyst on 16 Jul 2014
Description
This GUI will help the novice user get up to speed very quickly on using GUI-based applications. Everything is laid out in a very simple Step 1, Step 2, Step 3, etc. layout. It is a very good starting point for a typical image analysis application. This application uses GUIDE to do the user interface design, and has most of the basic controls such as buttons, listboxes, checkboxes, radio buttons, scrollbars, etc. It allows the user to select a folder of images, select one or more images and display them, to select a series of options, and to individually or batch process one or more images. The user can optionally apply a mask (region of interest) to the image so that only the area within the mask will be analyzed. The results are optionally sent to Excel. In this demo, I .................
Just replace the demo code in "AnalyzeSingleImage" with whatever code you want to run on your image. it pretty much handles the rest.

Community Treasure Hunt

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

Start Hunting!

Translated by