How can I include a repeated word into a matrix?

1 次查看(过去 30 天)
I have 2 matrices that look like this
2016_Subject_
2016_Subject_
2016_Subject_
_01
_02
_03
and I need to put the word 'Speed' in between each line - Expected answer is
2016_Subject_Speed_01
2016_Subject_Speed_02
2016_Subject_Speed_03
this code
[L(i,1:end-2), 'Speed', L(i,end-2:end),]
where L is ls(targetfolder) ie. a list of the subfolders in a main folder and looks like
2016_Subject_01
2016_Subject_02
2016_Subject_03
doesn't work and comes up with the error
"Error using horzcat Dimensions of matrices being concatenated are not consistent."
which is only due to the inclusion of the words.
Any ideas how to overcome this?
Thanks in advance
  3 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2016-7-7
编辑:Azzi Abdelmalek 2016-7-7
You can post the expected result, because "put the word 'Speed' in between each line" is not clear
Stephen23
Stephen23 2016-7-8
@Eleanor H: it is still not very clear exactly how your data are stored, but you can try experimenting with strcat to achieve what you want:
A = {...
'2016_Subject_'
'2016_Subject_'
'2016_Subject_'};
B = {...
'_01'
'_02'
'_03'};
strcat(A,'Speed',B)
creates this:
ans =
'2016_Subject_Speed_01 '
'2016_Subject_Speed_02'
'2016_Subject_Speed_03'

请先登录,再进行评论。

回答(2 个)

Thorsten
Thorsten 2016-7-7
编辑:Thorsten 2016-7-7
Use strcat
L1 = ['2016_Subject_'; '2016_Subject_'; '2016_Subject_']
L2 = ['_01'; '_02'; '_03'];
strcat(L1, 'Speed', L2)
It also works with cells
C1 = {'1_Subject_'; '10_Subject_'; '1000_Subject_'};
C2 = {'_01'; '_02'; '_1000'};
strcat(C1, 'Speed', C2)

José-Luis
José-Luis 2016-7-7
a = repmat('2016_Subject_',3,1);
b = ['_01'; '_02'; '_03'];
your_array = cat(2,a,repmat('Speed',size(a,1),1),b);

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by