Error using variable in a function with trial version

Hello,
I have a problem using my function Selection Sept. It told me that i use date as a function or a command but i don't know where. Why can't I name it in my script, use it in my function and then use it in my script again ? It worked on my friend computer who don't have the trial version but i do and on mine in doesn't work...
Thank you very much

1 Comment

Script :
A = CR1000LaL ;
A(:,2:26) = [];
A(1:4,:) = [];
date = A(:,1)
[A] = SelectionSept (A)
abe = A(1,:);
for l = length(date):-1:1;
for k = 2:length(abe);
while A{l,k} <= 2 || A{l,k} > 3 ;
date(l) = [];
A(l,:) = [];
end
end
end
Function
function [matrice] = SelectionSept (matrice)
for i = length(date):-1:1;
j= date{i};
token = strtok(j{1}(7));
if token ~= '9';
date(i)= [];
matrice(i,:) = [];
end
end
end
Error message : "date" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.

Sign in to comment.

 Accepted Answer

You should avoid using the variable name date, because this is the name of an inbuilt function date. You should always check if a name is used by calling this: which name_of_function.
The error is simply that you do not pass any date as an argument to your function. If date is not passed (or otherwise explicitly put into that function workspace) then it does not exist in that workspace. In MATLAB function workspaces do not automagically inherit all values from the calling or "higher" workspace: you have to pass them explicitly:
function [matrice] = SelectionSept(matrice,date)

More Answers (1)

function [matrice] = SelectionSept (matrice)
for i = length(date):-1:1;
...
date isn't defined in your function excepting as the builtin Matlab function
>> which date
C:\ML_R2014b\toolbox\matlab\timefun\date.m
because functions have their own context and the array date you created in the script isn't passed to the function.
It would behoove you to not use builtin function names in general; confusion reigns when alias something this way.
While I do NOT recommend it; the code above would function if the sundtion were defined to use date as the argument; then it would become associated with the A array passed internally. BUT, do NOT do this; use some other variable name other than DATE!!!

Tags

Community Treasure Hunt

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

Start Hunting!