MATLAB Answers


Indexing a matrix that's not in the workspace

Asked by Matthew Calvin on 31 Jul 2019
Latest activity Answered by TADA
on 31 Jul 2019
I'm reading through someone else's code and trying to understand it. One thing it would be very helpful to be able to do is to be able to see what entries are in specific places in certain matrixes without having to name them. So for example their code might have at some point:
What I'd like to be able to do in the Command Window is:
>> [1,2,3,4](3)
ans =
but the code as written doesn't work. Is there a way to do this other than naming the variable and then checking the position?


Sign in to comment.




1 Answer

Answer by TADA
on 31 Jul 2019
 Accepted Answer

you can write a utility function to do that
function value = debugPrint(a, i)
value = a(i);
%% or using an anonymous function:
debugPrint = @(a,i) a(i);
now you can call it like that:
debugPrint([1,2,3,4], 3)
ans =
or use the builtin indexing function subsref for that:
subsref([1,2,3,4], substruct('()', {3}))


Sign in to comment.