MATLAB:使用vertcat时出错。连接的矩阵的尺寸不一致

时间:2015-08-18 20:46:57

标签: matlab concatenation

我有两个包含我要连接的字符串的表。从串联到串联,字符串的长度会有所不同。我已检查所有变量是否匹配/尺寸匹配。

这是令人困惑的部分:我有两个相等维度的表和相同的变量,但我仍然会得到标题中列出的错误。简而言之:之前发生过这种错误或者我的代码是否还有其他事情发生(相反,这种错误是否可能)?如果之前发生了这种情况,那么有哪些修复?再说一遍:我已多次验证两个表的尺寸是否一致。

调用连接函数的顶级循环。

function saveAnnotation(obj)

    switch obj.annotationMode 
        case 1 
            isCellEmpty = isFieldEmpty(obj);

            if isCellEmpty 
                createNewAnnoTable(obj); 
            elseif ~isCellEmpty
                addToAnnoTable(obj); 
            end

            set(obj.regionSelector, 'Enable', 'off'); 

        case 2 
            isCellEmpty = isFieldEmpty(obj); 

            if isCellEmpty 
                createNewAnnoTable(obj); 
            elseif ~isCellEmpty
                addToAnnoTable(obj); 
            end 

        case 3 
            if obj.selectedLead == 1
                for i = 1:6 
                    fillRecPairInfo(obj, i); 
                    isCellEmpty = isFieldEmpty(obj, i); 
                    if isCellEmpty 
                        createNewAnnoTable(obj, i);
                    elseif ~isCellEmpty 
                        addToAnnoTable(obj, i); 
                    end

                end 
            elseif obj.selectedLead == 2
                for i = 7:12
                    fillRecPairInfo(obj, i); 
                    isCellEmpty = isFieldEmpty(obj, i); 
                    if isCellEmpty 
                        createNewAnnoTable(obj, i);
                    elseif ~isCellEmpty 
                        addToAnnoTable(obj, i);
                    end
                end
            end

        case 4
            for i = 1:12
                fillRecPairInfo(obj, i); 
                isCellEmpty = isFieldEmpty(obj, i); 
                if isCellEmpty
                    createNewAnnoTable(obj, i);
                elseif ~isCellEmpty 
                    addToAnnoTable(obj, i);
                end
            end
    end
    end

“isFieldEmpty”功能

function fieldEmpty = isFieldEmpty(obj, varargin)

    pairSelection = obj.selectedPair; 
    displayedPair = find(pairSelection(1:12) == 1); 


    switch obj.annotationMode
        case 1
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData);
                elseif displayedPair(i) > 6
                    fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData);
                end
            end
        case 2
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData);
                elseif displayedPair(i) > 6
                    fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData);
                end
            end
        case 3
            checkIndx = varargin{1};
            if checkIndx <= 6
                fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData); 
            elseif checkIndx > 6
                fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData); 
            end 

        case 4 
            checkIndx = varargin{1}; 
            if checkIndx <= 6
                fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData); 
            elseif checkIndx > 6
                fieldEmpty = isempty(obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData); 
            end 

    end
end

“createNewAnnoTable”功能

function createNewAnnoTable(obj, varargin)
    pairSelection = obj.selectedPair; 
    displayedPair = find(pairSelection(1:12) == 1); 

    switch obj.annotationMode
        case 1
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = obj.nextRow;
                elseif displayedPair(i) > 6
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = obj.nextRow; 
                end
            end

        case 2
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = obj.nextRow;
                elseif displayedPair(i) > 6
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = obj.nextRow; 
                end
            end

        case 3
            checkIndx = varargin{1};
            if checkIndx <= 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = obj.nextRow;
            elseif checkIndx > 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = obj.nextRow; 
            end

        case 4
            checkIndx = varargin{1};
            if checkIndx <= 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = obj.nextRow;
            elseif checkIndx > 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = obj.nextRow; 
            end

    end
end

“addToAnnoTable”功能

function addToAnnoTable(obj, varargin)


    switch obj.annotationMode 
        case 1
            pairSelection = obj.selectedPair; 
            displayedPair = find(pairSelection(1:12) == 1); 
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6
                    existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations;
                elseif displayedPair(i) > 6 
                    existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations; 
                end 
            end

            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = [existingTable; obj.nextRow];
                elseif displayedPair(i) > 6
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = [existingTable; obj.nextRow]; 
                end
            end

        case 2
            pairSelection = obj.selectedPair; 
            displayedPair = find(pairSelection(1:12) == 1); 
            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6
                    existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations;
                elseif displayedPair(i) > 6 
                    existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations; 
                end 
            end

            for i = 1:length(displayedPair)
                if displayedPair(i) <= 6 
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, displayedPair(i)).UserData.Annotations = [existingTable; obj.nextRow];
                elseif displayedPair(i) > 6
                    obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (displayedPair(i) - 6)).UserData.Annotations = [existingTable; obj.nextRow]; 
                end
            end

        case 3
            checkIndx = varargin{1};
            if checkIndx <= 6
                existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations;
            elseif checkIndx > 6 
                existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations; 
            end 

            if checkIndx <= 6 
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = [existingTable; obj.nextRow];
            elseif checkIndx > 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = [existingTable; obj.nextRow]; 
            end

        case 4
            checkIndx = varargin{1};
            if checkIndx <= 6
                existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations;
            elseif checkIndx > 6 
                existingTable = obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations; 
            end 

            if checkIndx <= 6 
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L1(1, checkIndx).UserData.Annotations = [existingTable; obj.nextRow];
            elseif checkIndx > 6
                obj.ephysData.sortedMontageMatrix.(obj.selectedSession).L2(1, (checkIndx - 6)).UserData.Annotations = [existingTable; obj.nextRow]; 
            end

     end     
end

1 个答案:

答案 0 :(得分:2)

经过进一步研究后,我意识到我的表格并不包含字符串。 它实际上包含独立字符串和字符串单元格的组合。 当MATLAB尝试连接独立字符串时,就是发生错误的时候。

我认为问题在于MATLAB处理的字符串类似于字符向量而不是单个对象,因此将其放在表中的单个列中会导致新字符串长度不同时出现连接问题。

我能够通过使每个独立字符串成为字符串单元来解决此问题。这解决了我的问题,因为现在MATLAB连接的是单元格而不是字符串。