How to use a for loop to iterate through a table?
조회 수: 128 (최근 30일)
이전 댓글 표시
Hi! I am very new to programming and I am trying to understand how to loop through a table. "indextable" is the table I am trying to loop through, and the values of indextable are the indices of cellnames. Hence I want 'i' to represent the values in indextable, rather than the indices. However, when I try the code below, rather than getting the appropriate values ex. (6, 9, 14), I get all the numbers from 6 to 14. How can I fix this? Thank you and I'm sorry if this is a simple question!
%%Make a polar plot
for column = 1:width(indextable) %columns in table
for i = indextable{1,column}:indextable{end,column};
cellname = cellnames{i}; %create looping variable
figure %create a new figure for each one
[firingRate,plottingangles]=Calculate_Firing_Rate(spiketimes(cellname),angleData); %FR function
polarplot(deg2rad(plottingangles),firingRate); %convert to radians
title(cellname);
end
end
댓글 수: 0
채택된 답변
dpb
2018년 4월 14일
Not sure I follow what you're after eggsactly but I'll take a fryer...
for i = indextable{1,column}:indextable{end,column};
for column=1 the first loop reduces to
for i = indextable{1,1}:indextable{end,1}
which is just
for i = indextable(1):indextable(end)
and since ':' means indextable(1):1:indextable(end) you do indeed get all elements from the first to the last.
What you're looking for instead is less complicated--again assuming your index array is just a vector of values and not a 2D array for some reason unspecified in what your overall objective is, then
id=[6 9 14]; % create the list; note it's just a vector, not a cell array
for i=1:length(idx)
...
reference the ith column-variable here in the table
end
It's not clear just what are the other elements in the call to your compute-engine function; seeing the result of
indextable.Properties.VariableNames
would give us the data for what is in the table and you could 'splain what is needed to compute what it is you're after and then what to plot...
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!