如何将来自两个不同元胞数组的数据存储到 MATLAB 中的一个结构字段中
How can I store data from two different cell arrays into one structure field in MATLAB
这是我的第一个:
假设我有一个结构
student_information=struct{'name','','surnames,''};
和两个元胞数组
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
我需要用 possible_names 元胞数组中的随机名称填充结构字段 'name',我认为:
for i=1:length(student_information)
for j=1:length(possible_names);
student_information(i).name=possible_names(randperm(j,1));
end
end
但我需要用 possible_surnames 元胞数组中的两个随机姓氏(即 "Gordon Maxwell")填充结构字段 'surnames'...我尝试了一种类似的方法我曾经填写 'names' 字段,但我没有。
非常感谢你的帮助
你的代码没有多大意义TBH。你有一些语法错误。具体来说:
student_information=struct{'name','','surnames,''};
'surnames'
需要结束语。这也只分配 one 结构。此外,struct
是一个函数,但您正试图将其用作元胞数组。你可能是这个意思:
student_information=struct('name','','surnames','');
除此之外:
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
这是无效的 MATLAB 语法。 cell
是一个函数,但您正试图将其作为 cell
数组来引用。改为这样做:
possible_names={'John','Amy','Paul'};
possible_surnames={'Mitchell','Gordon','Maxwell'};
现在,回到您的代码。从您的上下文来看,您想要 select 来自 possible_surnames
的两个随机名称并将它们连接成一个字符串。使用 randperm
但生成 两个 选项而不是代码中的 1 个选项。接下来,您可以使用 sprintf
并用 space:
分隔每个名称来作弊
possible_surnames={'Mitchell','Gordon','Maxwell'};
names = possible_surnames(randperm(numel(possible_surnames),2));
out = sprintf('%s ', names{:})
out =
Gordon Maxwell
因此,在填充元胞数组时,您可以像这样从 possible_names
数组中随机选择一个名称。首先,您需要使用给定数量的插槽正确分配结构:
student_information=struct('name','','surnames','');
student_information=repmat(student_information, 5, 1); %// Make a 5 element structure
possible_names={'John','Amy','Paul'};
for idx = 1 : numel(student_information)
student_information(idx).name = possible_names{randperm(numel(possible_names), 1)};
end
现在 possible_surnames
,执行:
possible_surnames={'Mitchell','Gordon','Maxwell'};
for idx = 1 : numel(student_information)
names = possible_surnames(randperm(numel(possible_surnames),2));
student_information(idx).surnames = sprintf('%s ', names{:});
end
让我们看看这个结构现在是什么样的:
cellData = struct2cell(student_information);
fprintf('Name: %s. Surnames: %s\n', cellData{:})
Name: Paul. Surnames: Maxwell Gordon
Name: Amy. Surnames: Mitchell Maxwell
Name: Paul. Surnames: Mitchell Gordon
Name: John. Surnames: Gordon Maxwell
Name: John. Surnames: Gordon Mitchell
这是我的第一个:
假设我有一个结构
student_information=struct{'name','','surnames,''};
和两个元胞数组
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
我需要用 possible_names 元胞数组中的随机名称填充结构字段 'name',我认为:
for i=1:length(student_information)
for j=1:length(possible_names);
student_information(i).name=possible_names(randperm(j,1));
end
end
但我需要用 possible_surnames 元胞数组中的两个随机姓氏(即 "Gordon Maxwell")填充结构字段 'surnames'...我尝试了一种类似的方法我曾经填写 'names' 字段,但我没有。
非常感谢你的帮助
你的代码没有多大意义TBH。你有一些语法错误。具体来说:
student_information=struct{'name','','surnames,''};
'surnames'
需要结束语。这也只分配 one 结构。此外,struct
是一个函数,但您正试图将其用作元胞数组。你可能是这个意思:
student_information=struct('name','','surnames','');
除此之外:
possible_names=cell{'John','Amy','Paul'};
possible_surnames=cell{'Mitchell','Gordon','Maxwell'};
这是无效的 MATLAB 语法。 cell
是一个函数,但您正试图将其作为 cell
数组来引用。改为这样做:
possible_names={'John','Amy','Paul'};
possible_surnames={'Mitchell','Gordon','Maxwell'};
现在,回到您的代码。从您的上下文来看,您想要 select 来自 possible_surnames
的两个随机名称并将它们连接成一个字符串。使用 randperm
但生成 两个 选项而不是代码中的 1 个选项。接下来,您可以使用 sprintf
并用 space:
possible_surnames={'Mitchell','Gordon','Maxwell'};
names = possible_surnames(randperm(numel(possible_surnames),2));
out = sprintf('%s ', names{:})
out =
Gordon Maxwell
因此,在填充元胞数组时,您可以像这样从 possible_names
数组中随机选择一个名称。首先,您需要使用给定数量的插槽正确分配结构:
student_information=struct('name','','surnames','');
student_information=repmat(student_information, 5, 1); %// Make a 5 element structure
possible_names={'John','Amy','Paul'};
for idx = 1 : numel(student_information)
student_information(idx).name = possible_names{randperm(numel(possible_names), 1)};
end
现在 possible_surnames
,执行:
possible_surnames={'Mitchell','Gordon','Maxwell'};
for idx = 1 : numel(student_information)
names = possible_surnames(randperm(numel(possible_surnames),2));
student_information(idx).surnames = sprintf('%s ', names{:});
end
让我们看看这个结构现在是什么样的:
cellData = struct2cell(student_information);
fprintf('Name: %s. Surnames: %s\n', cellData{:})
Name: Paul. Surnames: Maxwell Gordon
Name: Amy. Surnames: Mitchell Maxwell
Name: Paul. Surnames: Mitchell Gordon
Name: John. Surnames: Gordon Maxwell
Name: John. Surnames: Gordon Mitchell