필터 지우기
필터 지우기

how to concatenate two fields in a struct ?

조회 수: 9 (최근 30일)
sandy
sandy 2013년 9월 6일
hi...i want to concatenate fields namels data and text data below,how to do this? textdata is the label name for the maindata...any suggestion ?
struct(1*1)
maindata(138*173 double)
textdata(138*1 cell)

채택된 답변

Geert
Geert 2013년 9월 6일
Sandy,
it is still very unclear what you exactly want. In what format do you want your output data?
Do you want something like the following example?
cc = struct;
cc.maindata = [0 +1.193790E+1 +5.275883E+0 +1.796951E+1;
0 +1.188781E+1 +6.487981E+0 +1.919098E+1
0 +1.175415E+1 +5.493386E+0 +1.851824E+1];
cc.textdata = {'SAMPLE_20130606_1358'; 'SAMPLE_20130606_1408'; 'SAMPLE_20130606_1418'}
concatenateddata = cell(size(cc.maindata,1),size(cc.maindata,2) + size(cc.textdata,2));
for ii = 1:size(cc.maindata,1)
concatenateddata{ii,1} = cc.textdata{ii};
for jj=1:size(cc.maindata,2)
concatenateddata{ii,jj+1} = cc.maindata(ii,jj);
end
end
  댓글 수: 1
sandy
sandy 2013년 9월 6일
편집: sandy 2013년 9월 6일
YES...IT WORKS..thanks a lot Geert.THIS IS WHAT I WANT...can you see this question too < http://www.mathworks.in/matlabcentral/answers/86404-how-to-read-sts-file-in-matlab>

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

추가 답변 (1개)

Geert
Geert 2013년 9월 6일
Can you be more specific?
Do you want to convert the (double) data in maindata to a string, and concatenate it to the strings in the cell array textdata? In that case, here is an example code snippet on how to do it:
cc = struct;
cc.maindata = [1 2;3 4; 5 6];
cc.textdata = {'firststring'; 'secondstring'; 'thirdstring'}
concatenateddata = cell(3,1);
for ii = 1:size(cc.maindata,1)
concatenateddata{ii} = [num2str(cc.maindata(ii,:)),' ', cc.textdata{ii}];
end
Please make your question more understandable.
  댓글 수: 2
sandy
sandy 2013년 9월 6일
편집: sandy 2013년 9월 6일
HI...your code concatenating both to singel cell as output,but i need output in each cell(like horzcat()) ... inside textdata(138*1) present in each cell
SAMPLE_20130606_1358
SAMPLE_20130606_1408
SAMPLE_20130606_1418
.....
inisde maindata(138*173) in each cell
0 +1.193790E+1 +5.275883E+0 +1.796951E+1
0 +1.188781E+1 +6.487981E+0 +1.919098E+1
0 +1.175415E+1 +5.493386E+0 +1.851824E+1
i need to concatenate both,,where string and numeric in each cell of the output variable,it like below
SAMPLE_20130606_1358 0 +1.193790E+1 +5.275883E+0 +1.796951E+1
SAMPLE_20130606_1408 0 +1.188781E+1 +6.487981E+0 +1.919098E+1
SAMPLE_20130606_1418 0 +1.175415E+1 +5.493386E+0 +1.851824E+1
SAMPLE_20130606_1428 0 +1.282015E+1 +5.903331E+0 +1.901381E+1
SAMPLE_20130606_1438 0 +1.160890E+1 +5.647130E+0 +2.120915E+1
SAMPLE_20130606_1448 0 +1.158797E+1 +5.673629E+0 +1.841896E+1
sandy
sandy 2013년 9월 10일
there.i used importdata() to read my file and output as struct containing names and values as separate.so i used concatenate to join both to save in excel..

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by