Could you help me with FUNCTION syntax
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Peter
el 20 de Feb. de 2012
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 comentarios
Oleg Komarov
el 20 de Feb. de 2012
format the code please: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Respuesta aceptada
Walter Roberson
el 20 de Feb. de 2012
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 comentarios
Más respuestas (2)
Eric
el 20 de Feb. de 2012
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 comentarios
Walter Roberson
el 20 de Feb. de 2012
That's still the function syntax. An output argument not assigned has to do with the code inside the function.
Eric
el 20 de Feb. de 2012
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.
Ver también
Categorías
Más información sobre Interpolation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!