Create an array of strings

后端 未结 7 1921
走了就别回头了
走了就别回头了 2020-12-15 03:11

Is it possibe to create an array of strings in MATLAB within a for loop?

For example,

for i=1:10
Names(i)=\'Sample Text\';
end

I do

7条回答
  •  鱼传尺愫
    2020-12-15 03:43

    You can create a character array that does this via a loop:

    >> for i=1:10
    Names(i,:)='Sample Text';
    end
    >> Names
    
    Names =
    
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    

    However, this would be better implemented using REPMAT:

    >> Names = repmat('Sample Text', 10, 1)
    
    Names =
    
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    Sample Text
    

提交回复
热议问题