필터 지우기
필터 지우기

How to append data for a variable column in a matrix?

조회 수: 2 (최근 30일)
Ashish
Ashish 2014년 4월 30일
댓글: Cedric 2014년 4월 30일
I load an excel sheet using xlsread command. The excel sheet can have any number of rows and columns.
Example of the matrix (3*2)from the excel sheet will look like this:
System Code
AEA AEA00
AW WP000
The above matrix can have any length. Based on the column dimension, I want to concatenate the rows with a '-'. For e.g. the second row after concatenation can look like AEA-AEA00.
Could you suggest a way out for implementing the same?

채택된 답변

Cedric
Cedric 2014년 4월 30일
편집: Cedric 2014년 4월 30일
Assuming
D = {'AEA', 'AEA00'; 'AW', 'WP000'}
you can concatenate e.g. row 1 with
>> sprintf( '%s-', D{1,:} )
ans =
AEA-AEA00-
and remove the last dash afterwards. For performing this on the whole array:
nRows = size( D, 1 ) ;
Dcat = cell( nRows, 1 ) ;
for k = 1 : nRows
tmp = sprintf( '%s-', D{k,:} ) ;
Dcat{k} = tmp(1:end-1) ;
end
This works with an arbitrary number of rows and columns. In the present case, it outputs
>> Dcat
Dcat =
'AEA-AEA00'
'AW-WP000'
Notes:
  • You might want to truncate the first row if it contains 'System Code'.
  • This solution assumes that each row has the same number of non-empty columns. Let me know if it isn't the case; it is easy to add a line which selects only non-empty cells before the call to SPRINTF.
  댓글 수: 6
Ashish
Ashish 2014년 4월 30일
Perfect!
Thanks for the example and understanding my doubt clearly.
Cedric
Cedric 2014년 4월 30일
My pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by