我有一些 MATLAB 代码,是我和一个在 MATLAB 方面比我更有经验的人一起工作的。前几天代码运行得非常好。当我今天尝试运行它时,收到错误“这种类型的变量不支持点索引”,并且我尝试阅读有关该主题的许多其他堆栈溢出问题,但仍在努力解决我的问题。我将包含直到出现以下错误为止的代码。根据我读到的内容,这可能与结构有关?如果您能向我发送任何帮助或提示,我将不胜感激。谢谢!
for j = 3:length(files)
files(j).name
folder_name = files(j).folder;
file2load = strcat(folder_name, '\', files(j).name);
data_input = load(file2load);
trace_names = fieldnames(data_input);
for i = 1:length(trace_names)
data_to_plot.x = data_input.(char(trace_names(i))).data.x;
data_to_plot.y = data_input.(char(trace_names(i))).data.y;
正如评论所指出的,尚不清楚为什么您会收到此错误,但我可以告诉您是什么导致了此错误。 “点索引”是一种访问称为
struct
的 Matlab 变量类型中的数据字段的方法。在您的代码片段中,files(j)
不是struct
。
例如,您不能这样做:
>> s = {}; % s is a cell array (not a struct)
>> g = {};
>> g{1} = 'lala';
>> s{1} = g;
>> s.g % cannot access g using this syntax
Dot indexing is not supported for variables of this type.
但是如果你将
s
变成 struct
,你可以:
>> s = struct
s =
struct with no fields.
>> s.item1 = g; % create a field called item1 to hold the stuff in g
>> s.item1 % now you can use dot indexing to access the stuff you put in there
ans =
1×1 cell array
{'lala'}
我猜测这个片段是函数的一部分,其中
files
是参数,或者是先前定义 files
的脚本的一部分。可能有一天和你的同事一起,你将 files
设置为一个变量,该变量实际上是一个结构体数组,其中包含项目 name
等。从那时起,某些东西将 files
的值更改为非结构类型数组。