MATLAB Answers

daniel
0

indexing vectors in a nested for loop

Asked by daniel
on 6 Nov 2014
Latest activity Commented on by Hikaru
on 6 Nov 2014
Hi! so this is probably a simple fix but its not coming to me as fast as I'd like... seems easy enough. I have assigned some vectors from an excel file. I would like to go through each element in each vector, do some analysis, extract some info and put it back in a vector of the same size. My problem arises when trying to index my vectors.. I am using two for loops one for each vector and the other for each element within the vector.
data = xlsread('myfile.xlsx'):
wrd2_1 = data(:,1);
wrd2_2 = data(:,2); . . . wrd2_6 = data(:,6);
for ii = 1:length(wrd2_1)
for jj = 1:length(wrd2_1)
bins{jj} = dec2bin(wrd2_{ii}(ii));
lob = length(bins{jj});
diff = abs(16 - lob);
switch diff
case 1
v1 = strcat('0',bins{jj});
case 2
v1 = strcat('00',bins{jj});
case 3
v1 = strcat('000',bins{jj});
case 4
v1 = strcat('0000',bins{jj});
case 5
v1 = strcat('00000',bins{jj});
otherwise v1 = bins{jj};
end
some more stuff...
end
the problem arises here:
bins{jj} = dec2bin(wrd2_{ii}(ii));
Undefined variable "wrd2_" or class "wrd2_". Error in RDR_PLOTS (line 76) bins{jj} = dec2bin(wrd2_{ii}(jj));
I realize what I am doing wrong but I am not sure how to go about resolving this.
I know that I could put all of the data into one matrix and indexing would be fine but it is not ideal for my situation.
any help is appreciated thanks!

  1 Comment

Your variable names are wrd2_1, wrd2_2, wrd2_3,... wrd2_6, so you have to call them properly. As you pointed out, one matrix would've been fine. Mind telling us why it is not ideal?

Sign in to comment.

0 Answers