MATLAB: Error: subscript indices must either be real positive integers or logicals.

functionMATLAB

Hello,
this part of function doesn't working and I don't have any idea why.
hA=axes;
hold on
for i=1:length(Data)
hL(i)=plot(hA,Data(i).x,Data(i).y);
set(hL(i),'LineWidth',LW{i});
end
If I type this code in command window it work perfectly. If I use it as part of function it return:
Subscript indices must either be real positive integers or logicals.
Error in plotGraph (line 144)
set(hL(i),'LineWidth',LW{i});

Best Answer

It seems like you have created a variable called "set". Then the arguments are treated as indices and the string causes the error:
set = rand(100, 100, 3)
set(hL(i), 'LineWidth', LW{i});
% ^ Bad index!
The solution is easy: Do not use "set" as name of a variable.
To check if this is the problem, let Matlab stop, when the error occurs: Type this in the command window and run your code again:
dbstop if error
Now check:
which set -all
I guess, the topmost result is a local variable, and not Matlab's built-in function.