Return function the same as the inputs.

Hello every body,
I wrote a function to creat a curve and it worked fine and the function returns a vector of 1x1 size (see the attached image please). How to make this function return a vector nurbs of the same size as the input arguments?? Here is my function
function nurbs = nrbmak(coef,knots)
coefs = [0.0 1.5; 0.0 3.0];
knots = [0.0 0.0 1.0 1.0];
nurbs.form = 'B-NURBS';
nurbs.dim = 4;
np = size(coefs);
dim = np(1);
nurbs.number = np(2);
if (dim < 4)
nurbs.coefs = repmat([0.0 0.0 0.0 1.0]',[1 np(2)]);
nurbs.coefs(1:dim,:) = coefs;
else
nurbs.coefs = coefs;
end
nurbs.order = size(knots,2)-np(2);
knots = sort(knots);
nurbs.knots = (knots-knots(1))./(knots(end)-knots(1));
nrbctrlplot(nurbs);
end

6 comentarios

Kevin Chng
Kevin Chng el 11 de Oct. de 2018
Sorry, do you want your output same as input? I don't get it.
Stephen23
Stephen23 el 11 de Oct. de 2018
@Bell: what size should the output structure be? What are the input values? Please give us some example input and expected output arrays.
Bell
Bell el 11 de Oct. de 2018
Editada: Bell el 11 de Oct. de 2018
@Kevin Chng: The function must return a vector nurbs of the same size as the input arguments.
@Stephen Cobeldick: it should be 126. This function above works fine in MATLAB, but when I take this function to a simulation program (COMSOL), the programs does not accept that and says ''Incorrect size of returned vector. 126 elements were expected, but the returned matrix was 1 x 1''
Rik
Rik el 11 de Oct. de 2018
Mostly a duplicate of this question
Bell
Bell el 11 de Oct. de 2018
Yes I posted that. But it was not clear what I want to do. I'll delete the first question.
Guillaume
Guillaume el 11 de Oct. de 2018
"But it was not clear what I want to do. I'll delete the first question"
Then you edit the original question, not start a new question where we'll probably ask the same questions that have already been answered.

Iniciar sesión para comentar.

 Respuesta aceptada

Steven Lord
Steven Lord el 11 de Oct. de 2018

0 votos

Your nurbs variable is a scalar struct array. If you want it to be a non-scalar struct array (of size [1 126]) we can do that. But I think what you want is to use that scalar struct array to evaluate the non-uniform rational B-spline at a vector of points.
I would check the functions given in the Splines chapter in the documentation for Curve Fitting Toolbox work for your application, allowing you to create a spline without having to manually construct the struct yourself. If they do, and your goal is to evaluate the spline, see the Postprocessing section for some tools that may be useful to you.

Más respuestas (1)

Guillaume
Guillaume el 11 de Oct. de 2018
Your function makes no sense. It takes two inputs, coef and knots, and the first thing it does is discard that knots input and replace it by a constant value. And if the coef vs coefs is a typo, it would also discard the coef input.
If coefs and knots are the default values that should be used if these aren't provided, then you need to check that they indeed have not been provided:
function nurbs = nrbmak(coefs, knots)
if nargin < 2 %knots not provided
knots = [0.0 0.0 1.0 1.0];
end
if nargin < 1 %coef not provided
coefs = [0.0 1.5; 0.0 3.0];
end
nurbs.form = 'B-NURBS';
nurbs.dim = 4;
nurbs.number = size(coefs, 2);
%simpler code than your if else:
nurb.coefs(end+1:3, :) = 0; %if matrix is less than 3 rows fill missing rows with 0
nurb.coefs(end+1:4, :) = 1; %if row 4 is not provided, fill with 1.
nurbs.order = size(knots,2)-np(2);
knots = sort(knots);
nurbs.knots = (knots-knots(1))./(knots(end)-knots(1));
nrbctrlplot(nurbs);
end

Categorías

Preguntada:

el 11 de Oct. de 2018

Respondida:

el 11 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by