seperation columns when one string in number other is txt.

names=['john'; 'ravi'; 'mary'; 'xiao']
howdy=char('10', '20', '30')
result=[howdy(2,:) names(3,:)]
result=20mary
I wanna create "20 marry" with spaces between columns, how can I add the spaces between the columns?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 27 Mai 2013
result=[howdy(2,:), ' ', names(3,:)];
or use sprintf(), like I prefer:
result = sprintf('%s %s', howdy(2,:), names(3,:));
By the way, if any names are not exactly the same length as the others, then you'll have to have names be a cell array, not a character array and you'll have to use braces, not parentheses, like I did here.

4 Kommentare

but when I try to result=[howdy(:,1), ' ', a(:,1)] horzcat error occurs how can I fix this error?
or result=[howdy, ' ', a]
a=names
Well you switched from rows like you started (and I answered) to columns. Look - the indexes inside howdy and a went from the first index being specified to the second index being specified. So now you're trying to stitch together horizontally vertical columns that have different heights. You can't do that because the matrix would no longer be rectangular - it would have an irregular bottom edge.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by