在 Matlab 中使用 strrep 去除双引号
Using strrep to remove double quotes in Matlab
我有一个包含 41 个字段的 1x1 结构 (EEG)。其中一个字段称为 event 并且是一个 1x450 结构,具有不同的字段,其中一些是字符串和一些数值。我想删除出现在字符串 fields/columns 中的双引号。例如,我有一个名为 type 的列,其中包含 '"acc"'、"bacc"' 等,我想将字符串转换为 'acc'、'bacc'、等等
我试过:
strrep(EEG.event.type, '"','');
它 returns 这个错误:
Error using strrep
Too many input arguments.
我也试过直接select要去掉双引号的列:
strrep(EEG.event(:,[3:4 6:10]),'"','');
它给了我这个错误:
Error using strrep
Conversion to double from struct is not possible.
解决结构字段可以包含任何内容这一事实的一种方法是编写一个 for 循环来替换需要它的字段。以下是一个非常通用的循环,它将覆盖结构的所有字段,检查它们是否为字符串并进行替换:
names = fieldnames(EEG.event)
for e = 1:numel(EEG.event)
for f = 1:numel(names)
if ischar(EEG.event(e).(names{f}))
strrep(EEG.event(e).(names{f}), '"', '')
end
end
end
注意动态的使用field names and a precomputed list of fields. You may also find the following question and answer useful: Iterating through struct fieldnames in MATLAB
如果 EEG.event.type 是数组中每个 EEG.event 结构的字符串(正如您的输出所示),您可以这样做:
for i=1:numel(EEG.event)
EEG.event(i).type = strrep(EEG.event(i).type,'"','')
end
这还假设您只是试图修改 type 字段和其他字段的 none,尽管它也适用于其他字段,如果它们是相同的格式。如果您事先知道您正试图修改哪些字段并且它们是正确的类型,那么您就不必遍历结构中的所有字段。
编辑开始
遗憾的是,我不知道有任何非循环方法可以同时作用于结构数组的多个字段。除了 structfun() 之外,我还没有找到允许您执行此操作的内置函数,它仅适用于标量结构。通常我倾向于避免结构数组,出于这个原因更喜欢数组结构,但根据你的问题,我猜你的数据来自外部源并且你无法控制格式。您可以随时执行以下操作之一:
fieldsToModify = {'type','someOtherField','yetAnotherField'};
for i=1:numel(EEG.event)
for j=1:numel(fieldsToModify)
EEG.event(i).(fieldsToModify{j}) = strrep(EEG.event(i).(fieldsToModify{j}),'"','');
end
end
这将使您不必检查所有 字段,但不会使您免于嵌套的 for 循环。你也可以这样做:
allFields = fieldnames(EEG.event)
for i=1:numel(EEG.event)
for j=1:numel(allFields)
switch(allFields{j})
case {'type','someOtherField','yetAnotherField'}:
EEG.event(i).(allFields{j}) = strrep(EEG.event(i).(allFields{j}),'"','');
%Should you discover other fields that need modification
case {list of other fields to modify}:
% Some function to modify them
otherwise:
% Default action
end
end
end
同样,由于嵌套的 for 循环并不理想,但如果您发现有更多字段需要根据您的目的进行修改,那么至少以后修改起来会更容易。
最后,最不优雅的解决方案:
for i=1:numel(EEG.event)
EEG.event(i).type = strrep(EEG.event(i).type,'"','');
EEG.event(i).field1 = strrep(EEG.event(i).field1,'"','');
% And so on for all the fields that need to be modified
end
我有一个包含 41 个字段的 1x1 结构 (EEG)。其中一个字段称为 event 并且是一个 1x450 结构,具有不同的字段,其中一些是字符串和一些数值。我想删除出现在字符串 fields/columns 中的双引号。例如,我有一个名为 type 的列,其中包含 '"acc"'、"bacc"' 等,我想将字符串转换为 'acc'、'bacc'、等等
我试过:
strrep(EEG.event.type, '"','');
它 returns 这个错误:
Error using strrep
Too many input arguments.
我也试过直接select要去掉双引号的列:
strrep(EEG.event(:,[3:4 6:10]),'"','');
它给了我这个错误:
Error using strrep
Conversion to double from struct is not possible.
解决结构字段可以包含任何内容这一事实的一种方法是编写一个 for 循环来替换需要它的字段。以下是一个非常通用的循环,它将覆盖结构的所有字段,检查它们是否为字符串并进行替换:
names = fieldnames(EEG.event)
for e = 1:numel(EEG.event)
for f = 1:numel(names)
if ischar(EEG.event(e).(names{f}))
strrep(EEG.event(e).(names{f}), '"', '')
end
end
end
注意动态的使用field names and a precomputed list of fields. You may also find the following question and answer useful: Iterating through struct fieldnames in MATLAB
如果 EEG.event.type 是数组中每个 EEG.event 结构的字符串(正如您的输出所示),您可以这样做:
for i=1:numel(EEG.event)
EEG.event(i).type = strrep(EEG.event(i).type,'"','')
end
这还假设您只是试图修改 type 字段和其他字段的 none,尽管它也适用于其他字段,如果它们是相同的格式。如果您事先知道您正试图修改哪些字段并且它们是正确的类型,那么您就不必遍历结构中的所有字段。
编辑开始
遗憾的是,我不知道有任何非循环方法可以同时作用于结构数组的多个字段。除了 structfun() 之外,我还没有找到允许您执行此操作的内置函数,它仅适用于标量结构。通常我倾向于避免结构数组,出于这个原因更喜欢数组结构,但根据你的问题,我猜你的数据来自外部源并且你无法控制格式。您可以随时执行以下操作之一:
fieldsToModify = {'type','someOtherField','yetAnotherField'};
for i=1:numel(EEG.event)
for j=1:numel(fieldsToModify)
EEG.event(i).(fieldsToModify{j}) = strrep(EEG.event(i).(fieldsToModify{j}),'"','');
end
end
这将使您不必检查所有 字段,但不会使您免于嵌套的 for 循环。你也可以这样做:
allFields = fieldnames(EEG.event)
for i=1:numel(EEG.event)
for j=1:numel(allFields)
switch(allFields{j})
case {'type','someOtherField','yetAnotherField'}:
EEG.event(i).(allFields{j}) = strrep(EEG.event(i).(allFields{j}),'"','');
%Should you discover other fields that need modification
case {list of other fields to modify}:
% Some function to modify them
otherwise:
% Default action
end
end
end
同样,由于嵌套的 for 循环并不理想,但如果您发现有更多字段需要根据您的目的进行修改,那么至少以后修改起来会更容易。
最后,最不优雅的解决方案:
for i=1:numel(EEG.event)
EEG.event(i).type = strrep(EEG.event(i).type,'"','');
EEG.event(i).field1 = strrep(EEG.event(i).field1,'"','');
% And so on for all the fields that need to be modified
end