0

I have a Matlab code as you can see here :

    function IWDalg(similarityMatrix,NumberOfSentencesInFile,NumberOfSentencesInAbstract)
NumIWDs = str2int(NumberOfSentencesInFile);
Numnodes=NumIWDs;

av = 1; bv = 0.01; cv = 1;
as = 1; bs = 0.01; cs = 1;

soil = repmat(InitSoil,Numnodes,Numnodes);
for i =1:NumIWDs
    IWD{i}.vel = InitVel;
    IWD{i}.tour = [];
    IWD{i}.tour(1) =i;
    IWD{i}.soil = 0;

end

I the loop for when the matlab tries to compile first line of For clause i got this error:

??? For colon operator with char operands, first and last operands must be char.

I am so beginner in matlab programming .

Best regards

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • How should we answer this without knowledge what `NumIWDs` is? Obviously, it is something `char`-like, but to be sure, you must tell us. – glglgl Mar 31 '14 at 12:22
  • Well I am unsure of how you performed `str2int`. Is this a function created by you? I could not find it on my version of matlab (2013a). In that case it would be nice with some test code since hidden homemade functions must be specified if someone declare he got an error. – patrik Mar 31 '14 at 12:25
  • No it is matlab's function – Ehsan Akbar Mar 31 '14 at 12:26
  • On a side note, you should generally not use `i` and `j` as variables in Matlab for reasons detailed in [this question](http://stackoverflow.com/questions/14790740/using-i-and-j-as-variables-in-matlab) – David K Mar 31 '14 at 12:27
  • @DavidK There are no problem with using `i` or `j` as iteration variables. The conversion from complex gives almost no overhead and only a minimum of increased runtime. Also, assignment is normally not what have the largest impact of runtime. It is also bad practice to use `i` or `j` as complex values since `1i` or `1j` should b used instead – patrik Mar 31 '14 at 12:36
  • @patrik You are right that if you consistently use `i` and `1i` properly it will not cause errors. That being said, getting in the habit of using both is poor practice and just asking for one little typo to break your code. – David K Mar 31 '14 at 12:47
  • @DavidK Sure the using `i` in the wrong place may cause errors, but using `1i` will never cause any trouble except for the irritation of having two different notations for the same thing if `i` is used as well. This is why `1i` should be used all the time instead of `i`. But there are already a thread about this, so this discussion should be held there instead – patrik Mar 31 '14 at 14:10

1 Answers1

2

Try this .

NumIWDs = str2double(NumberOfSentencesInFile);

You should convert char to double for executing for colon.

  • 1
    This is of course a solution, but **int** works as well for me. The problem is most likely that `str2int` does something else than what it is supposed to do. – patrik Mar 31 '14 at 12:29