Could you help me with FUNCTION syntax

1 回表示 (過去 30 日間)
Peter
Peter 2012 年 2 月 20 日
編集済み: Andrea 2013 年 10 月 17 日
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.
  1 件のコメント
Peter
Peter 2012 年 2 月 24 日
Thank you very much for your time and help :-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by