I am trying to make a table of numbers with corresponding names for example: names and grade table sorted by the number
Bob 97
Joe 87
Sally 88
How do i setup a variable to equal something like a= Bob 97 so when i go to do the fprintf it will sort by the numbers in the second column and leave the corresponding names to the number in the first column
final table should look like:
Name Grade
Joe 87
Sally 88
Bob 97

2 件のコメント

per isakson
per isakson 2012 年 4 月 29 日
Why not int2str?
Image Analyst
Image Analyst 2012 年 4 月 29 日
Because he wants it sorted by grade, and the grade doesn't necessarily show up in the same column, or even in a single column, if the names have different lengths:
>> int2str('Sally 88')
ans =
83 97 108 108 121 32 32 32 56 56

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2012 年 4 月 29 日

0 投票

Ryan, if you can read in from your input (say, with textscan) and stuff the name into a cell array and stuff the numbers into a double array, then you can sort the double array and sort the cell array the same way, like in this (untested) code:
% Create sample data:
names = {'Bob'; 'Joe'; 'Sally'}
grades = [97 87 88]
% Now sort by ascending grade number:
[sortedGrades sortOrder] = sort(grades, 'ascend');
% Sort the names the same way.
sortedNames = names(sortOrder);
% Now print out to the command window.
fprintf('Name Grade\n');
for k = 1 : length(sortedGrades)
fprintf('%s %d\n', sortedNames{k}, sortedGrades(k));
end
In the command window:
names =
'Bob'
'Joe'
'Sally'
grades =
97 87 88
Name Grade
Joe 87
Sally 88
Bob 97

3 件のコメント

Ryan Grace
Ryan Grace 2012 年 4 月 29 日
thanks a bunch that helped me out a lot. Now one more question if you don't mind. How do i make the table "pretty"? the numbers are all out of line, any advice?
ratios=[0.265 0.322 0.310 .230 .288 .271 .318];
names={'Common Dolphin';'Fraser''s Dolphin';'Atlantic Spotted Dolphin';'Bottlenose Dolphin';'Rough Toothed Dolphin';'Striped Dolphin';'Risso''s Dolphin'};
[sortedratios sortOrder] = sort(ratios, 'ascend');
sortedNames = names(sortOrder);
fprintf('Name Ratio,r\n')
for k = 1 : length(sortedratios)
fprintf('%s %6.3f\n', sortedNames{k}, sortedratios(k));
end
that is my code and this is the printed table:
Name Ratio,r
Bottlenose Dolphin 0.230
Common Dolphin 0.265
Striped Dolphin 0.271
Rough Toothed Dolphin 0.288
Atlantic Spotted Dolphin 0.310
Risso's Dolphin 0.318
Fraser's Dolphin 0.322
Image Analyst
Image Analyst 2012 年 4 月 29 日
Yeah, that's hard, unless you want to use a uitable. The tab (\t) is somewhat unreliable as to where it places the next column. What I do is to set the font to a fixed spacing font like Courier (I know, Courier is not "pretty" though) and then use fixed length fields, e.g. %20s or %7d to line things up.
Ryan Grace
Ryan Grace 2012 年 4 月 29 日
Ya i just used a number in front of the %s and %f and it looks decent enough for what i am doing. Thanks for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Data Preprocessing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by