how can i run a .m file pressing a push button ?

1 view (last 30 days)
Farjana Yeasmin
Farjana Yeasmin on 25 Nov 2014
Edited: Adam on 26 Nov 2014
my code of the funtion is :
function cropping_fun(image, x_TopLeft, y_TopLeft, x_BottomRight, y_BottomRight)
clc; close all;
% let us handle cases if the user inputs incorrect or insufficient inputs
if nargin>0 if (ischar(image))&&(exist(image, 'file')==2) A=double(imread(image)); elseif exist('image', 'var') A=double(image); end size_A=size(A); end
if nargin==0 A=double(imread('D:\\Thesis Code\\Source Code\\StegoImage.png')); size_A=size(A); x_TopLeft=ceil(size_A(1)/4); y_TopLeft=ceil(size_A(2)/4); x_BottomRight=floor(3*size_A(1)/4); y_BottomRight=floor(3*size_A(2)/4); elseif nargin==1 x_TopLeft=ceil(size_A(1)/4); y_TopLeft=ceil(size_A(2)/4); x_BottomRight=floor(3*size_A(1)/4); y_BottomRight=floor(3*size_A(2)/4); elseif nargin==2 y_TopLeft=ceil(size_A(2)/4); x_BottomRight=floor(3*size_A(1)/4); y_BottomRight=floor(3*size_A(2)/4); elseif nargin==3 x_BottomRight=floor(3*size_A(1)/4); y_BottomRight=floor(3*size_A(2)/4); elseif nargin==4 y_BottomRight=floor(3*size_A(2)/4); end
%==========================================================================
% size of the cropped image
M=x_BottomRight-x_TopLeft+1; N=y_BottomRight-y_TopLeft+1;
%==========================================================================
% Memory allocation for the output image for faster implementation of the % code
image_dim=size(size_A);
if image_dim(2)==3 P=3; B=zeros(M, N, P); else B=zeros(M, N); end
%==========================================================================
if ((M>0)&&(N>0))
for i=1:M
for j=1:N
B(i, j, :)=A(x_TopLeft+i, y_TopLeft+j, :);
end
end
new_imagesc(A);
title('Original image');
new_imagesc(B);
title('Cropped image');
else
error('Check the dimensions of the cropped image.');
end
end % End of the cropping_fun
%==========================================================================
function new_imagesc(A)
%==========================================================================
% Author: Prashant Athavale % Date 11/19/2011 % Please acknowledge my name if you use this code, thank you. % This is a function to display images at the center of the screen using % Matlab's imagesc function
%==========================================================================
figure; screen_size=get(0,'screensize'); screen_width=screen_size(3); screen_height=screen_size(4); figure_width=screen_size(4)/2; % width of the figure figure_height=screen_size(4)/2; % height of the figure x_lowerbottom=(screen_width-figure_width)/2; y_lowerbottom=(screen_height-figure_height)/2; set(gcf,'position',[x_lowerbottom y_lowerbottom figure_width figure_height]); image_dim=size(size(A));
% What to do if the image is color/RGB or grayscale
if image_dim(2)==3 M=max(max(max(A))); elseif image_dim(2)==2 M=max(max(A)); end
if M>1
M=1;
else
M=255;
end
% imagesc likes the images from 0 to 255 % if they are not then we make them so.
A=double(A); imagesc(uint8(A*M)); if image_dim(2)==3 colormap('colormap'); else colormap('gray'); end
end % end of function new_imagesc
%==========================================================================

Answers (1)

Adam
Adam on 25 Nov 2014
Edited: Adam on 25 Nov 2014
Just call cropping_fun from your push button callback and pass it the 5 required arguments.
You haven't given enough information to know where you get those 5 arguments from or what you want to do with the output from calling your function so that is really all the advice I can give without wasting time giving advice you don't even need.
As an aside 'clc' is not generally very useful in a function and would generally be considered an undesirable side effect of calling a function that purports to just crop something, but I guess that is your business!
  2 Comments
Farjana Yeasmin
Farjana Yeasmin on 25 Nov 2014
i can't understand , i am a beginner of matlab .. will u plz say it more clearly ? thank you
Adam
Adam on 26 Nov 2014
Edited: Adam on 26 Nov 2014
You literally just type:
cropping_fun(image, x_TopLeft, y_TopLeft, x_BottomRight, y_BottomRight)
in your push button callback, supplying all those inputs as arguments. I don't know where you intend to get those input arguments from.
If you don't know how to work with callbacks I suggest you look in the Matlab help at e.g.

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!