Merge elements of a row into a single element array

19 Ansichten (letzte 30 Tage)
JohnDylon
JohnDylon am 11 Sep. 2016
Kommentiert: JohnDylon am 11 Sep. 2016
Hi,
I want to merge each row of CHAR type matrix
BB=[C I M G 2 8 3 0; C I M G 2 8 3 2; C I M G 2 8 3 3; C I M G 2 8 3 4]
as
BBB=[CIMG2830; CIMG2832; CIMG2833; CIMG2834]
or in other words I want to merge each row into one single element, not necessarily all elements are numbers.
Any ideas?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 11 Sep. 2016
BB=['C' 'I' 'M' 'G' '2' '8' '3' '0'; 'C' 'I' 'M' 'G' '2' '8' '3' '2'; 'C' 'I' 'M' 'G' '2' '8' '3' '3'; 'C' 'I' 'M' 'G' '2' '8' '3' '4']
is already completely equivalent to
BBB = ['CIMG2830'; 'CIMG2832'; 'CIMG2833'; 'CIMG2834']
Are you sure you are starting with a char matrix? And not, perhaps, a cell array whose elements are each single char ? If what you have is
BB={'C' 'I' 'M' 'G' '2' '8' '3' '0'; 'C' 'I' 'M' 'G' '2' '8' '3' '2'; 'C' 'I' 'M' 'G' '2' '8' '3' '3'; 'C' 'I' 'M' 'G' '2' '8' '3' '4'}
then
BBB = reshape( horzcat(BB{:}), size(BB,1), [])
  3 Kommentare
Walter Roberson
Walter Roberson am 11 Sep. 2016
Bearbeitet: Walter Roberson am 11 Sep. 2016
Do not use cell2mat() for that purpose. Instead, use
char(aa)
as that will convert the cell array of strings into a row-based array of char.
However, you would seldom need that. You would typically instead use something like,
filename = aa{k};
fullname = fullfile(folder, filename);
JohnDylon
JohnDylon am 11 Sep. 2016
That worked great for me. Thank you for pointing a torch on this.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by