MATLAB Answers


how to create a for loop 380:780?

Asked by vicsm
on 13 Sep 2019 at 19:39
Latest activity Commented on by vicsm
on 13 Sep 2019 at 23:07
how can I create a for loop between in this statement
CM4_380 = spec_data(spec_data.wavelength>380 & spec_data.wavelength<381,:);
so it for loops between 380:780? Basically, to make this:
CM4_381 = spec_data(spec_data.wavelength>381 & spec_data.wavelength<382,:);
CM4_382 = spec_data(spec_data.wavelength>382 & spec_data.wavelength<383,:);
CM4_383 = spec_data(spec_data.wavelength>383 & spec_data.wavelength<384,:);
etc. Plus I need to save each of those tables.
Thank you for your help!


Sign in to comment.


1 Answer

Answer by the cyclist
on 13 Sep 2019 at 20:00
 Accepted Answer

I would use a cell array, not variable names that also encode an index:
for ic = 380:780
CM4{ic} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);


the cyclist
on 13 Sep 2019 at 21:07
So, is there still a problem? Your code runs to completion for me, and fills in the cells I would expect.
Walter Roberson
on 13 Sep 2019 at 21:23
I would suggest
for ic = 380:780
CM4{ic-380+1} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);
unless you have a use for CM4{1} to CM4{379}
on 13 Sep 2019 at 23:07
I does work, thank you!

Sign in to comment.