Weird problem with MATLAB misbehaving..

3 views (last 30 days)
MSP
MSP on 1 Jan 2018
Commented: Walter Roberson on 1 Jan 2018
This is the Linear regression function. My problem is when I am running one line using this function MATLAB2017a returns values but when this function is inside another function it does not run leading to error. Pls have a look at the attached snap
function [a1,a0] = linreg(x,lsqsize,lsqshift)
error(nargchk(1,3,nargin)) % # INPUTS BET'N 1 AND 3
% DEFAULT VALUES
if nargin == 1
lsqsize = 7;
lsqshift = 1;
elseif nargin == 2
lsqshift = 1;
end
num_col = fix((size(x,1) - lsqsize + lsqshift)/lsqshift);
num_row = size(x,2);
for k = 1:num_col
for l = 1:num_row
loc = (k-1)*lsqshift+1;
[a1(k,l),a0(k,l)] = lsqfit(x(loc:loc+lsqsize-1,l));
end
end
end

Answers (1)

Image Analyst
Image Analyst on 1 Jan 2018
Put these lines as the first two lines inside your linreg() function:
a0 = []; % Initialize to null.
a1 = []; % Initialize to null.
Then set a breakpoint at this line
[a1(k,l),a0(k,l)] = lsqfit(x(loc:loc+lsqsize-1,l));
and then try to figure out why your code execution never ever reaches that line of code and so a1 never gets assigned at all.
  3 Comments
Walter Roberson
Walter Roberson on 1 Jan 2018
In your code being called within the function, either num_row or num_col is coming out as 0 because of the data you pass in.
I note by the way that you are calling LinReg but your function name is linreg . MATLAB is case sensitive; however, the name of the file overrides the name given on the function line.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by