MATLAB Answers

Extract Previous Cell Value

6 views (last 30 days)
Jeffrey Pang
Jeffrey Pang on 21 Apr 2019
Commented: TADA on 23 Apr 2019
I have a cell with char values next to each other as seen below
Cell 1 | Cell 2
'Hello' | 'World'
I would like to define a function that extracts the previous cell value along with the current cell value. I wrote the following code to do so:
t = x{1,i} - Current Cell Value
f = x{2,i-1} - Previous Cell Value, starting with Cell 2 with the previous value of Cell 1
But I keep getting the error
Index in position 2 is invalid. Array indices must be positive integers or logical values.
I believe the error is with regards to the defined function f. It has to do with the i-1 value. At this point, I am stuck. Any thoughts on how to fix that so it works?


Sign in to comment.

Answers (1)

TADA on 21 Apr 2019
Edited: TADA on 21 Apr 2019
The problem is you're indexing at index 0 which is invalid.
% some specific handling of
% the first cell should be
% executed here if necessary
for i = 2:size(x,2)
t = x{1,i};
f = x{2,i-1};


Show 3 older comments
TADA on 21 Apr 2019
This is your problem
f = x{2,j-1};
As x is a one dimentional row cell array
This should work I think:
for i = 1:52
t = x{1,i};
f = x{1,i-1};
if t(1) == f(1)
Jeffrey Pang
Jeffrey Pang on 23 Apr 2019
I got it. Thank you for putting me on the right track!
TADA on 23 Apr 2019
Good luck

Sign in to comment.

Sign in to answer this question.

Translated by