MATLAB: ()-indexing must appear last in an index expression.

for loop

I created code that works fine if it is not in a for loop. When I make it a loop, I get the error:
Error: File: plotAllchrom.m Line: 6 Column: 35 ()-indexing must appear last in an index expression.
I know this is common, but as a new user I cannot figure out where to change to curly braces.
Thanks!!
clc
%load as vectors
for i = 1:14;
ds = dataset(chrom, pos, x3D7_A10P, x3D7_812, x3D7_712, x3D7_5391, Dd2_TM1, Dd2_TM2, Dd2_PQR);
ds_chr(i)= ds(ds.chrom==(i),:);
meancov_A10P_chr(i) = mean(double(ds_chr(i)(:,3:3)),1);
meancov_812_chr(i) = mean(double(ds_chr(i)(:,4:4)),1);
meancov_712_chr(i) = mean(double(ds_chr(i)(:,5:5)),1);
meancov_5391_chr(i) = mean(double(ds_chr(i)(:,6:6)),1);
meancov_TM1_chr(i) = mean(double(ds_chr(i)(:,7:7)),1);
meancov_TM2_chr(i) = mean(double(ds_chr(i)(:,8:8)),1);
meancov_PQR_chr(i) = mean(double(ds_chr(i)(:,9:9)),1);
norm_A10P_chr(i) = (double(ds_chr(i)(:,3:3)))/meancov_A10P_chr(i);
norm_812_chr(i) = (double(ds_chr(i)(:,4:4)))/meancov_812_chr(i);
norm_712_chr(i) = (double(ds_chr(i)(:,5:5)))/meancov_712_chr(i);
norm_5391_chr(i) = (double(ds_chr(i)(:,6:6)))/meancov_5391_chr(i);
norm_TM1_chr(i) = (double(ds_chr(i)(:,7:7)))/meancov_TM1_chr(i);
norm_TM2_chr(i) = (double(ds_chr(i)(:,8:8)))/meancov_TM2_chr(i);
norm_PQR_chr(i) = (double(ds_chr(i)(:,9:9)))/meancov_PQR_chr(i);
T=table(norm_A10P_chr(i), norm_812_chr(i), norm_712_chr(i), norm_5391_chr(i));
A=table2array(T);
calibrate=nanmean(A,2);
cal_A10P_chr(i)=norm_A10P_chr(i)./calibrate;
cal_812_chr(i)=norm_812_chr(i)./calibrate;
cal_712_chr(i)=norm_712_chr(i)./calibrate;
cal_5391_chr(i)=norm_5391_chr(i)./calibrate;
cal_TM1_chr(i)=norm_TM1_chr(i)./calibrate;
cal_TM2_chr(i)=norm_TM2_chr(i)./calibrate;
cal_PQR_chr(i)=norm_PQR_chr(i)./calibrate;
figure
pos_chr(i)=(double(ds_chr(i)(:,2:2)));
hold on;
h(1) = subplot(7,2,1);
scatter(pos_chr(i), cal_A10P_chr(i), 'filled', 'blue');
scatter(pos_chr(i), cal_812_chr(i), 'filled', 'green');
scatter(pos_chr(i), cal_712_chr(i), 'filled', 'red');
scatter(pos_chr(i), cal_5391_chr(i), 'filled', 'cyan');
scatter(pos_chr(i), cal_TM1_chr(i), 'filled', 'yellow');
scatter(pos_chr(i), cal_TM2_chr(i), 'filled', 'magenta');
scatter(pos_chr(i), cal_PQR_chr(i), 'filled', 'black');
legend('A10P', '812', '712', '5391', 'TM1', 'TM2', 'PQR');
hold off;
end

Best Answer

Your variable ds_chr seems to be a cell array. Every (or most) reference to ds_chr{k} should use curly braces, like this: ds_chr{i}. This will work:
ds_chr{k}(:,3:3)
whereas this is an error:
ds_chr(k)(:,3:3)
The concept for indexing cell arrays is simple:
  • {} curly braces access the data inside the cells.
  • () parentheses access the cells themselves.
Just think of cell arrays like boxes: do you want to pick up the box (the cell, ()), or whatever that is inside the box (the data, {}). Sometimes you want to pick up the box, and sometimes you want to get out whatever is inside... each of these can be useful.
Also note that you should avoid using i and j as loop variable names, as these are both names of the inbuilt imaginary unit .