Display numbered cell summary?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Guillaume
le 22 Oct 2014
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 commentaires
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!