MATLAB Answers

How can I convert following script to function?

14 views (last 30 days)
Sel
Sel on 4 Aug 2015
Edited: Al Dente on 4 Aug 2015
Hi all,
I am having trouble converting following script to function. Function need to have xxx(image,factor). My script is below.
a
=imread('cameraman.tif');
[m n] = size(a);
p = 2;
for i=1:m %loop to extract every row
for j=1:n %loop to extract every column
for k=1:p %loop to control the number of replication
b(i,(j-1)*p+k)=a(i,j); %replication pf pixels in row wise
end
end
end
c=b;
[m n]=size(c);
for i=1:n %loop to extract every column
for j=1:m %loop to extract every row
for k=1:p %loop to control the number of replication
b((j-1)*p+k,i)=c(j,i); %replication pf pixels in row wise
end
end
end
imshow (a), title('Original Image')
figure, imshow(b), title('Resized Image')
xlabel(sprintf('Resizing factor is %g', p))

  0 Comments

Sign in to comment.

Answers (1)

Al Dente
Al Dente on 4 Aug 2015
function xxx(image,p)
a=imread(image);
[m n] = size(a);
% remove this line p = 2;
if you don't want to change much of your code then take the above function header I guess, where image is the image you want to use.

  3 Comments

Sel
Sel on 4 Aug 2015
Hi,
Thanks for your answer. I dont want to change the code so much and when I do that I am getting
Undefined function 'xxx' for input arguments of type 'char'. Code is below..
function x = xxx(image, p)
a = imread(image);
[m n] = size(a);
for i=1:m %loop to extract every row for j=1:n %loop to extract every column for k=1:p %loop to control the number of replication b(i,(j-1)*p+k)=a(i,j); %replication pf pixels in row wise end end end
c=b; [m n]=size(c); for i=1:n %loop to extract every column for j=1:m %loop to extract every row for k=1:p %loop to control the number of replication b((j-1)*p+k,i)=c(j,i); %replication pf pixels in row wise end end end
imshow (x), title('Original Image') figure, imshow(b), title('Resized Image') xlabel(sprintf('Resizing factor is %g', p)) end
Al Dente
Al Dente on 4 Aug 2015
did you put your entire code in an m file and save it? does the m file exist in matlab path? -- the file name must be xxx.m in this case
Al Dente
Al Dente on 4 Aug 2015
function xxx(image,p)
a=imread(image);
[m n] = size(a);
%p = 2;
for i=1:m %loop to extract every row
for j=1:n %loop to extract every column
for k=1:p %loop to control the number of replication
b(i,(j-1)*p+k)=a(i,j); %replication pf pixels in row wise
end
end
end
c=b;
[m n]=size(c);
for i=1:n %loop to extract every column
for j=1:m %loop to extract every row
for k=1:p %loop to control the number of replication
b((j-1)*p+k,i)=c(j,i); %replication pf pixels in row wise
end
end
end
imshow (a), title('Original Image')
figure, imshow(b), title('Resized Image')
xlabel(sprintf('Resizing factor is %g', p))
this should be your code basically, put that in a new m file and save it as xxx.m and make sure to have it in your matlab path -- see addpath.
then you can xxx('cameraman.tif', 2) -- 'cameraman.tif' has to be in your matlab path as well, if not then use the entire path --> 'c:\foo\bar\cameraman.tif'

Sign in to comment.