Could you help me with FUNCTION syntax

1 次查看(过去 30 天)
Hi, could you help me please, how to make a function - for example
function [xx,yy,xx1,yy1,a,aa,b,bb]=(x,y,x1,y1)
(this is I tryed and it doesnt work :-( )
I have a points in my code (predefined - x,y,x1,y1) and I want change this points for a function (its mean write my own coordinate of points to the command window). Then I Interpolate and approximate this points by spline and polyfit.
Here is my code syntax (it works good, but I wanna make this like a function).. Thanks for any help, I just learning in Matlab so use simply answer :-)
CODE:
obr = imread('Pokus1.jpg');
obr = obr(:,:,1);
obr = im2double(obr);
figure(1)
imshow(obr,[]);
[a,b]=size(obr);
x=[19.25 ; 217.25 ; 523.25 ; 806.75 ; 1135.25 ; 1345.25 ; 1612.25];
y=[545.75 ; 364.25 ; 155.75 ; 92.75 ; 145.25 ; 256.25 ; 418.25];
x1=[163.25 ; 361.25 ; 778.25 ; 1007.75 ; 1175.75 ; 1409.75];
y1=[572.75 ; 358.25 ; 178.25 ; 200.75 ; 265.25 ; 425.75];
hold on
axis on
plot (x,y,'o');
xx = 1:0.01:b;
yy = spline(x,y,xx);
hold on
plot(x,y,'o',xx,yy, 'LineWidth', 3);
plot (x1,y1,'o');
xx1 = 1:0.01:b;
yy1 = spline (x1,y1,xx1);
hold on
plot(x1,y1,'o',xx1,yy1, 'LineWidth', 3);
hold off
figure(2)
imshow(obr,[]);
p = polyfit(x,y,2);
a = 1:0.01:b;
aa = polyval (p,a);
hold on
axis on
plot(x,y,'o',a,aa, 'LineWidth', 3);
p1 = polyfit(x1,y1,2);
b = 1:0.01:b;
bb = polyval (p1,b);
plot(x1,y1,'o',b,bb, 'LineWidth', 3);
hold off
  2 个评论
Oleg Komarov
Oleg Komarov 2012-2-20
format the code please: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Peter
Peter 2012-2-20
I did it :-) Could you help me please??

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2012-2-20
The line
function [xx,yy,xx1,yy1,a,aa,b,bb]=(x,y,x1,y1)
needs to have the function name added between the "=" and the "("

更多回答(2 个)

Eric
Eric 2012-2-20
First you need to give the function a name. Here I've called it my_function() and you should save it as my_function.m somewhere on the Matlab path.
function [xx,yy,xx1,yy1,a,aa,b,bb]=my_function(x,y,x1,y1)
-Eric
  3 个评论
Walter Roberson
Walter Roberson 2012-2-20
That's still the function syntax. An output argument not assigned has to do with the code inside the function.
Peter
Peter 2012-2-20
And could you help me how to fix it? When you look above on my code, how to make function? I would like to delete my own points (x,y,x1,y1) and use the function - write my own points..:-)

请先登录,再进行评论。


Eric
Eric 2012-2-20
I'm not entirely sure what you want this function to do. How about something like this? From the Matlab command prompt:
x=[19.25 ; 217.25 ; 523.25 ; 806.75 ; 1135.25 ; 1345.25 ; 1612.25];
y=[545.75 ; 364.25 ; 155.75 ; 92.75 ; 145.25 ; 256.25 ; 418.25];
x1=[163.25 ; 361.25 ; 778.25 ; 1007.75 ; 1175.75 ; 1409.75];
y1=[572.75 ; 358.25 ; 178.25 ; 200.75 ; 265.25 ; 425.75];
Then in my_function.m:
function [xx,yy,xx1,yy1,a,aa,b,bb]=my_function(x,y,x1,y1)
obr = imread('moon.tif');
obr = im2double(obr);
[a,b]=size(obr);
xx = 1:0.01:b;
yy = spline(x,y,xx);
xx1 = 1:0.01:b;
yy1 = spline (x1,y1,xx1);
p = polyfit(x,y,2);
a = 1:0.01:b;
aa = polyval (p,a);
p1 = polyfit(x1,y1,2);
b = 1:0.01:b;
bb = polyval (p1,b);
return
Then from the command prompt again:
[xx,yy,xx1,yy1,a,aa,b,bb]=my_function(x,y,x1,y1);
You might also want to pass the image in as a variable. Also, I've switched to moon.tif since I don't have your image.

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by