Could you help me with FUNCTION syntax
조회 수: 2 (최근 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
2012년 2월 20일
format the code please: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
채택된 답변
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 "("
댓글 수: 0
추가 답변 (2개)
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
2012년 2월 20일
That's still the function syntax. An output argument not assigned has to do with the code inside the function.
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 Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!