access multiple cell array values and compare with a matrix

2 views (last 30 days)
%no of steps
nsteps=30;
%no of species
ncell=3;
for c=1:ncell
cell{c}=c;
end
[B C D]=deal(cell{:});
I have generated matrix containing values mentioned above
A=zeros(ncell,nsteps);
A(B,1)=0;
A(C,1)=1;
A(D,1)=0;
I have a input matrix (a logic table) in text format IL1.txt
B C D
0 0 0
0 1 1
1 0 0
1 1 0
IL1_data = importdata('IL1.txt','\t');
for t=1:nsteps-1
for n=1:size(IL1_data.data,1)
if ((IL1_data.data(n,1:(end-1)))==[A(cell2mat(IL1_data.textdata(1,1:2)),t)])
A(cell2mat(IL1_data.textdata(1,3)),t+1)=(IL1_data.data(n,end));
break
end
end
end
it should work like:
for t=1:nsteps-1
for n=1:size(IL1_data.data,1)
if ((IL1_data.data(n,1:(end-1)))==[A(B,t) A(C,t)])
A(D,t+1)=(IL1_data.data(n,end));
break
end
end
end
But it is giving an error: Index exceeds matrix dimensions
Thank you.
  1 Comment
Guillaume
Guillaume on 18 Jun 2015
Calling a variable cell is a very bad idea since it shadows the cell function for creating cell arrays.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 18 Jun 2015
the problem comes from this:
A(cell2mat(IL1_data.textdata(1,1:2)),t)
which is equivalent to
A('BC', t)
If you pass a string as an index it is converted into its numerical value which is the ASCII value of the characters, so really what you're asking is:
A([66 67], t)
I'm not clear exactly on what you're trying to do with the variables B, C and D and the string headers. If theses variables are always 1, 2, and 3 respectively, one workaround would be to do:
indices = cell2mat(IL1_data.textdata(1, 1:2)) - 'A';
A(indices, t)
If what you really want is that string to refer to the actual variables B and C, then you could use eval, but I would not recommend going down that route:
indices = eval(sprintf('[%c %c]', IL1_data.textdata{1, 1:2}))
  2 Comments
Pooja Dnyane
Pooja Dnyane on 18 Jun 2015
Edited: Pooja Dnyane on 18 Jun 2015
Thank you very much for your reply.
I am numbering B,C and D such that A(B,1) is A(1,1) and trying to use string header (same variable) to access values from matrix A. So, every time with different table you don't have to provide input manually.
what if we use Bcell Ccell Dcell or variables with other names instead of using B,C and D
Guillaume
Guillaume on 18 Jun 2015
If the rows in A are always going to be in the order 'B', 'C', 'D', etc. then my first workaround would be the way to go (subtract the ascii value of 'A' from the name of the columns in your text file.
Another fairly elegant solution (but with some overhead) is to use tables instead:
nsteps = 30;
ncells = 3;
A = array2table(zeros(ncells, nsteps), 'RowNames', {'B', 'C', 'D'}, ...
'VariableNames', arrayfun(@(s) sprintf('step_%02d', s), 1:nsteps, 'UniformOutput', false));
A{{'B', 'C', 'D'}, 1} = [0 1 0]';
IL1_data = importdata('IL1.txt','\t');
for t = 1:nsteps
matchingrow = ismember(IL1_data.data(:, 1:2), A{IL1_data.textdata(1:2), t}', 'rows');
if sum(matchingrow) == 1 %found one row that match
A{IL1_data.textdata(3), t+1} = IL1_data.data(matchingrow, 3);
end
end
Note that the code above does exactly the same as your pseudocode, that is put in the next column the value of 'D' corresponding to the 'B' and 'C' of the current column. As it never touches rows 'B' and 'C', I don't think it's the result you want.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!