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

MATLAB: Error using vertcat. Dimensions of matrices being concatenated are not consistent

我有两个包含要连接的字符串的表。字符串的长度因连接而异。我检查了所有变量 match/dimensions 匹配。

这是令人困惑的部分:我有两个具有相同变量的相同维度的表,但我仍然会得到标题中列出的错误。简而言之:这种错误以前是否发生过,或者我的代码是否有其他问题(更确切地说,是否可能出现这种错误)?如果以前发生过这种情况,有哪些修复措施?再次:我已经多次验证两个表的尺寸是否一致。

Top-Level 调用连接函数的循环。

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

经过进一步研究,我意识到我的 table 不只包含字符串。 它实际上包含独立字符串和字符串单元的组合。当 MATLAB 尝试连接独立字符串时,即会发生错误。

我想问题在于 MATLAB 处理字符串的方式类似于字符向量而不是单个对象,因此当新字符串的长度不同时,将其放在 table 中的单个列中可能会导致连接问题。

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