a problem about sprintf, array and concetnation

i need a hope about this code, there's a mistake but i just can't figured it out..
i need the result:
out2=
[SemenPadang vs persiwa, SemenPadang vs persipura, PSPS vs ... SemenPadang, bye, Persisam vs semen padang;
PSPS vs Persipura, PSPS vs Persiwa, PSPS vs SemenPadang, bye, ...
BontangFc vs PSPS ]
teams = {'SemenPadang','PSPS','Sriwijaya','Persija','PelitaJaya','Persib','Persijap','Persibo','Arema','Persema','Persela','Deltras','Persiba','PSM','BontangFC','Persisam','Persiwa','Persipura'};
out2 =
[17 18 -2 0 -3 -4 5 6 -7 -8 9 10 -11 -12 13 14 -15 -16;
18 17 1 0 -4 -3 6 5 -8 -7 10 9 -12 -11 14 13 -16 -15 ]
ab = cat(2, teams, teams);
[brs, klm]= size(out2);
for b2= 1:brs
for k2= 1:klm
if out2(b2, k2) > 0
out2(b2, k2)= sprintf('%s vs %s\n', ab{b2}, ab{abs(out2(b2, k2))}); % when out2 is positive,
elseif out2(b2, k2) < 0
out2(b2, k2)= sprintf('%s vs %s\n', ab{abs(out2(b2, k2))}, ab{b2}); % it work's on single elements
else
out2(b2, k2)= sprintf('bye');
end
end
end
thx for attention and guidance..

2 Comments

Please format the code as explained in the "Markup help" link.
What exactly is the "mistake"? Is "[ semen padang vs ..." the wanted or the unwanted result and which format does this have? Is it a string?
thx simon,
pardon me, i try to concatenate between string value of "teams" through array of "out2" so i can make an array result's "out2", the final purpose was i can put the results's displayed on uitable..
since i have no another solution, i just put those operation 'coz it work's on a single element's..

Sign in to comment.

 Accepted Answer

sprintf() results in strings, which are character arrays. You cannot store an entire character array into a single location of a numeric array such as out2. In order to store a character array into a location designated with just two subscripts, you have to be storing into a cell array, such as
out3{b2, k2} = sprintf('%s vs %s\n', ab{abs(out2(b2, k2))}, ab{b2});

1 Comment

thx walter, it make another option for me, i'll tried it ..

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!