Matlab:多个结构元素的字段引用后面跟着更多的引用块是错误的

Matlab:多个结构元素的字段引用后面跟着更多的引用块是错误的,matlab,Matlab,我想让其他人的密码生效。我的主要问题是,我只有MatlabR2006B,而他似乎使用了更新的版本。(例如,我不得不将ClearVar的一些外观更改为clear)。我现在在下面的代码片段中遇到了一个问题 function [ oData ] = sync( iData ) cType = {'text1' 'text2' 'text3' 'text4'}; for i = 1:4 j = 1; while eval(['iData.' cType{i} '(j,6)']) >

我想让其他人的密码生效。我的主要问题是,我只有MatlabR2006B,而他似乎使用了更新的版本。(例如,我不得不将ClearVar的一些外观更改为clear)。我现在在下面的代码片段中遇到了一个问题

function [ oData ] = sync( iData )
cType = {'text1' 'text2' 'text3' 'text4'};

for i = 1:4
    j = 1;
    while eval(['iData.' cType{i} '(j,6)']) > 30000
        j = j+1;
    end
    eval(['iData.' cType{i} ' = iData.' cType{i} '(j:end,:);']);
end
这里我得到了错误

??? Error using ==> eval
Field reference for multiple structure elements that is followed by more reference blocks is an error.

Error in ==> sync at 7
    while eval(['iData.' cType{i} '(j,6)']) > 30000
有什么建议吗

编辑:

iData.text1

ans=

ans=


一种解决方案是使用
getfield
/
setfield
而不是
eval
,我认为这样更好:

iData.text1 = rand(10);
iData.text2 = rand(10);
iData.text3 = rand(10);
iData.text4 = rand(10);

cType = {'text1' 'text2' 'text3' 'text4'};
for i = 1:4
    j = 1;
    while getfield(iData, cType{i})(j,6) > 30000
        j = j+1;
    end
    setfield(iData, cType{i},...
             getfield(iData, cType{i})(j:end,:));
end

但是我没有在您的Matlab版本上试用。

由于结构不是标量(1x1),您必须添加一个额外的循环来逐步遍历结构的每个标量元素

在Matlab中也存在使用eval是不必要的

function [ oData ] = sync( iData )
cType = {'text1' 'text2' 'text3' 'text4'};

for kk=1:numel(iData)
    for ii = 1:4
        jj = 1;
        while iData(kk).(cType{ii})(jj,6) > 30000
            jj = jj+1;
        end
        iData(kk).(cType{ii})= iData(kk).(cType{ii})(jj:end,:);
    end
end

end

如果matlab不是真的需要,试着使用(OSS克隆)。什么是
size(iData)
?我假设代码是为1x1编写的。该错误表示它大于1x1,如果大于1x1,则在较新版本上也会出现该错误。6x1 struct array with fields i发布了一个答案,将其转换为处理非标量结构,但最近的编辑表明,非标量结构是错误,因为只有一个
text1
字段包含任何内容。我建议看看iData是如何创建的,因为它很可能是错误的
iData.text1 = rand(10);
iData.text2 = rand(10);
iData.text3 = rand(10);
iData.text4 = rand(10);

cType = {'text1' 'text2' 'text3' 'text4'};
for i = 1:4
    j = 1;
    while getfield(iData, cType{i})(j,6) > 30000
        j = j+1;
    end
    setfield(iData, cType{i},...
             getfield(iData, cType{i})(j:end,:));
end
function [ oData ] = sync( iData )
cType = {'text1' 'text2' 'text3' 'text4'};

for kk=1:numel(iData)
    for ii = 1:4
        jj = 1;
        while iData(kk).(cType{ii})(jj,6) > 30000
            jj = jj+1;
        end
        iData(kk).(cType{ii})= iData(kk).(cType{ii})(jj:end,:);
    end
end

end