Search code examples
wolfram-mathematicamathematica-8

set::write Tag Times Error for reading in files in For loop


I have a fairly simple code that I keep getting "set::write Tag Times in npsRadSlice is protected" errors for, despite having no real math in the code. I've gone through many other questions relating to the same error, and the solutions to those problems don't seem to work for me. Here's a look at my code:

Clear["Global'*"]
SetDirectory["C:\\Users\\M\\Desktop\\radial_slices"]
swank = 0.9;
angle = 0;
percDev = ConstantArray[1,3];
For[zz=1,zz<4,zz++
 Clear[npsRadSlice]
 npsRadSlice = Import[StringJoin["slice1_",ToString[101000+zz-1],".xlsx"]];
]

I've played around with different ways of calling the files in, but nothing changes the fact that I wind up with the error messages:

Set::write : Tag Times in npsRadSlice Null is protected.
Set::write : Tag Times in 2 npsRadSlice Null is protected.
Set::write : Tag Times in 3 npsRadSlice Null is protected.
General::stop : Further output of Set::write will be suppressed during this calculation.

I don't understand what is causing this error when there is no math anywhere in this code. Any insight you can give is greatly appreciated!


Solution

  • Your code is missing a comma and a semicolon

    swank = 0.9;
    angle = 0;
    percDev = ConstantArray[1, 3];
    For[zz = 1, zz < 4, zz++, 
     Clear[npsRadSlice];
     npsRadSlice = 1;
    ]
    

    To understand what is triggering the error, please look at the following. While this here

    zz++ 
    Clear[npsRadSlice] 
    npsRadSlice = 1
    

    is working, the next one is not

    Module[{},
     zz++ 
     Clear[npsRadSlice] 
     npsRadSlice = 1
    ]
    

    The rule of thumb is: Complete expressions at file level on different lines are regarded as separate expression. In other cases, there is an implicit multiplication.

    So basically, what you tried to do is something like this:

    z = 2;
    z*npsRadSlice = 3