Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 添加到单元格数组的末尾_Matlab - Fatal编程技术网

Matlab 添加到单元格数组的末尾

Matlab 添加到单元格数组的末尾,matlab,Matlab,我有一个12X1单元数组,它在每个单元中保存字符串。我还定义了四个字符串变量,我希望将它们添加到单元格数组的末尾,使其成为一个16X1数组。我已经在下面的代码中介绍了如何附加这四个变量;但我正在尽可能地学习这门语言,我很好奇是否有一种比我所写的更优雅的方式来附加这些变量 elseif (ext == '.s3p') %Read in file to be analyzed; convert to string format so %

我有一个12X1单元数组,它在每个单元中保存字符串。我还定义了四个字符串变量,我希望将它们添加到单元格数组的末尾,使其成为一个16X1数组。我已经在下面的代码中介绍了如何附加这四个变量;但我正在尽可能地学习这门语言,我很好奇是否有一种比我所写的更优雅的方式来附加这些变量

        elseif (ext == '.s3p')
            %Read in file to be analyzed; convert to string format so
            %that the strsplit function can be used
            inFile = textread(thisFile, '%s', 'delimiter', '\n'); %#ok<DTXTRD>   
            strForm = string(inFile);
            cellOut = arrayfun(@(x) strsplit(x, ' '), strForm, 'UniformOutput', false);

            %Find the end of the meta data and where the Option line of
            %the .s3p file begins (denoted by a '#'). Convert Option
            %line to string for parsing.
            metaDataEndLine = find(~cellfun(@isempty, strfind(inFile, 'Measurements'))) - 1; %#ok<STRCLFH>
            opLine = find(~cellfun(@isempty, strfind(inFile, '#'))); %#ok<STRCLFH>
            opStr = cellstr(cellOut{opLine});

            %Generate meta-data character strings to be printed to the
            %output file. File format can be generalized such that each
            %column corresponds to spefied data unit (i.e. frequency is
            %always the second col).
            metaFreq   = opStr(1,2);
            metaParam  = opStr(1,3);
            metaFormat = opStr(1,4);
            metaResist = opStr(1,6);

            %Genereate a cell array that houses the up-front data
            %provided by the file. Remove unnecessary chars.
            metaDataPrintFile = strForm(1:metaDataEndLine);
            metaDataPrintFile = cellfun(@(x)erase (x, '!'), metaDataPrintFile, 'UniformOutput', false);
            metaDataPrintFile = cellfun(@(x)erase (x, 'Correction: '), metaDataPrintFile, 'UniformOutput', false);

            %Add data to the class array metaPrint. Append string
            %variables generated above.
            this.metaPrint = metaDataPrintFile;
            this.metaPrint{length(this.metaPrint) + 1}      = strcat('Freq = ', metaFreq);
            this.metaPrint{length(this.metaPrint) + 2} = strcat('Parameter = ', metaParam);
            this.metaPrint{length(this.metaPrint) + 3} = strcat('Format = ', metaFormat);
            this.metaPrint{length(this.metaPrint) + 4} = strcat('Input Resistance = ', metaResist);     
        end %if
另外,如果你看到我能做些什么来提高我的代码效率,我洗耳恭听


提前谢谢你,如果我能提供任何其他信息,请告诉我

这是您的代码,简化了:

c = cell(12,1);
c{length(c) + 1} = 'a';
c{length(c) + 2} = 'b';
c{length(c) + 3} = 'c';
c{length(c) + 4} = 'd';
让我们看看它的功能:

length(c)
% ans =  22
哎呀!第一行添加一个元素;第二行在末尾添加一个元素2,然后添加一个空元素和字符串;第三行在末尾添加一个元素3,添加两个空元素,然后添加字符串;等等-请注意,每次都会在扩展阵列上计算lengthc

您要做的是:

c = cell(12,1);
c{length(c) + 1} = 'a';
c{length(c) + 1} = 'b';
c{length(c) + 1} = 'c';
c{length(c) + 1} = 'd';
或者更简单地说

c = cell(12,1);
c{end + 1} = 'a';
c{end + 1} = 'b';
c{end + 1} = 'c';
c{end + 1} = 'd';
这里我们每次都附加一个元素。要防止将数组大小增加4倍,可以按相反顺序分配:

c = cell(12,1);
c{end + 4} = 'd';
c{end - 1} = 'c';
c{end - 2} = 'b';
c{end - 3} = 'a';
这次我们在第一个语句中添加4个元素,然后再填充它们

另一种选择是

c = cell(12,1);
c(end + (1:4)) = {'a','b','c','d'};

请注意此处用于索引的圆括号。我们现在一次添加了4个元素。

太棒了!这正是我想要的。