Display numbered cell summary?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hey everyone. I am doing some hw and am just about finished with my code. Our professor wants it to output this.
Part Direction Distance
__________________
1 N 5
2 E 4
3 N 4
4 W 2
5 N 1
6 S 2
~~~~~Final Location of Balloon~~~~~~
N 8 E 2
This is what I have for the code that is supposed to output that.
fprintf('Part Direction Distance\n')
for k=1:n
fprintf('%d %s\n',k,BalloonCellArray{k})
end
FinalDirection = '';
fprintf('~~~~~Final Location of Balloon~~~~~~\n');
if(VerticalLocation~=0)
fprintf(' %s %g',VerticalDirection,VerticalLocation);
end
if(HorizontalLocation ~=0)
fprintf(' %s %g\n',HorizontalDirection,HorizontalLocation);
end
if(VerticalLocation==0)&&(HorizontalLocation==0)
fprintf(' NO CHANGE\n');
end
But this is what it outputs instead.
Part Direction Distance
1 N
2 E
3 N
4 W
5 N
6 S
~~~~~Final Location of Balloon~~~~~~
N 8 E 2
I am trying to display a 6x2 cell called BalloonCellArray and I think I'm really close. Thanks for any help!
댓글 수: 0
답변 (1개)
Guillaume
2014년 10월 22일
You're using linear indexing to index into a 2d cell array, so assuming that n is the number of rows, you're only indexing the first row. Use row, column indexing and index both columns in your loop. That is replace your fprintf with:
fprintf('%d %s %d\n', k, BalloonCellArray{k, 1}, BalloonCellArray{k, 2});
Note that using expansion of cell arrays into comma separated lists, the above can also be replaced with:
fprintf('%d %s %d\n', k, BalloonCellArray{k, :});
If you do use that second, you'd better put a comment explaining what's happening (by linking to the above doc for example).
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!