Return function the same as the inputs.

조회 수: 7 (최근 30일)
Bell
Bell 2018년 10월 11일
답변: Guillaume 2018년 10월 11일
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
Bell
Bell 2018년 10월 11일
Yes I posted that. But it was not clear what I want to do. I'll delete the first question.
Guillaume
Guillaume 2018년 10월 11일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2018년 10월 11일
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.

추가 답변 (1개)

Guillaume
Guillaume 2018년 10월 11일
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

카테고리

Help CenterFile Exchange에서 Spline Construction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by