Using polyfit subscripts in editor?

1 view (last 30 days)
Brenten
Brenten on 6 Dec 2013
Answered: John D'Errico on 6 Dec 2013
Hi,
I'm trying to write a script in editor which calls for a polyfit function, but it always returns an error that the subscript must be positive integer or logical. I've tried changing all the variables in the function, but it wont change the error. I'm not sure how to fix it. The code used to work in the command window.
Part of the code where it trips:
ind=find(abs(mzf-mz(rng)') > 7); mz(rng(ind))=mzf(ind); rng=imn-50:imn+50; warning off pp=polyfit(p(rng),mz(rng)',3); warning on mzf=polyval(pp,p(rng)); ind=find(abs(mzf-mz(rng)') > 7); mz(rng(ind))=mzf(ind);
Error is in bold. I've asked my team mates and a lecturer, but no answers as yet.
Thanks!

Answers (3)

Image Analyst
Image Analyst on 6 Dec 2013
You forgot to give the error. Please copy and paste all the red text - don't snip/clip/paraphrase it - give all of it to us.
Then, before the pp= line where the error occurs, put these lines:
rng % Don't use a semicolon so it will print to command window.
whos rng
Tell us what it says in the command window. If rng is huge, then just put rng(1:10) or some small chunk of it.

Brenten
Brenten on 6 Dec 2013
My bad.
So when I run the script upedited in the command window, this is what I see:
>> sweeper(SA,FZ,FY,MZ,MX,RL,IA,P,AMBTMP)
Subscript indices must either be real positive integers or logicals.
Error in sweeper (line 87) pp=polyfit(p(rng),mz(rng)',3);
With the 'whos rng' line:
>> sweeper(SA,FZ,FY,MZ,MX,RL,IA,P,AMBTMP)
rng =
Columns 1 through 13
155 156 157 158 159 160 161 162 163 164 165 166 167
Columns 14 through 26
168 169 170 171 172 173 174 175 176 177 178 179 180
Columns 27 through 39
181 182 183 184 185 186 187 188 189 190 191 192 193
Columns 40 through 52
194 195 196 197 198 199 200 201 202 203 204 205 206
Columns 53 through 65
207 208 209 210 211 212 213 214 215 216 217 218 219
Columns 66 through 78
220 221 222 223 224 225 226 227 228 229 230 231 232
Columns 79 through 91
233 234 235 236 237 238 239 240 241 242 243 244 245
Columns 92 through 101
246 247 248 249 250 251 252 253 254 255
rng =
Columns 1 through 13
224 225 226 227 228 229 230 231 232 233 234 235 236
Columns 14 through 26
237 238 239 240 241 242 243 244 245 246 247 248 249
Columns 27 through 39
250 251 252 253 254 255 256 257 258 259 260 261 262
Columns 40 through 52
263 264 265 266 267 268 269 270 271 272 273 274 275
Columns 53 through 65
276 277 278 279 280 281 282 283 284 285 286 287 288
Columns 66 through 78
289 290 291 292 293 294 295 296 297 298 299 300 301
Columns 79 through 91
302 303 304 305 306 307 308 309 310 311 312 313 314
Columns 92 through 101
315 316 317 318 319 320 321 322 323 324
Subscript indices must either be real positive integers or logicals.
Error in sweeper (line 90) pp=polyfit(p(rng),mz(rng)',3);

John D'Errico
John D'Errico on 6 Dec 2013
There are TWO possibilities, and only you can resolve it.
...
Columns 66 through 78
289 290 291 292 293 294 295 296 297 298 299 300 301
Columns 79 through 91
302 303 304 305 306 307 308 309 310 311 312 313 314
Columns 92 through 101
315 316 317 318 319 320 321 322 323 324
Subscript indices must either be real positive integers or logicals.
Error in sweeper (line 90) pp=polyfit(p(rng),mz(rng)',3);
I've excerpted some of what you have given us, the important part.
1. rng is not composed of integers, and you are trying to subscript into arrays p and mz with a non-integer subscript.
Test for the above by something like this:
any(rng ~= round(rng))
or
any(rem(rng,1) ~= 0)
Put this line in before the call to polyfit, or use the debugger to step through the code up to that line. Or set
dbstop if error
before you run the code. This will drop you into the debugger when the error occurs.
2. The second possibility, is that you have created an array with the name polyfit. This will confuse matlab and cause that error message. DON'T DO IT.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!