我有几个文件夹。每个文件夹中都有1个名为Data的文件。每个文件都有结构,其中之一就是Data.ensemble。从该结构中提取温度(第9列,请参见问题Hierarchy between and / or in an if statement)。我的问题现在在循环中,每次迭代都将温度提取并保存到结构中。例如,如果我有10个文件夹和10个文件。我要
Temperature.1=[12 15 10...]
Temperature.2=[20 30 11...]
...
Temperature.10=[15 26 27...]
,这些大小都会不同。提取工作正常,正在保存它是一个问题。我在https://nl.mathworks.com/matlabcentral/answers/304405-create-a-struct-iteratively
的帮助下尝试了以下代码folders =struct2cell(dir(Pathfolders)); %Pathfolders is the path to the folders
NaamFolders=string(char(folders(1,:))); %make a list of all the foldernames
lenNF=length(NaamFolders);
for i = 1:lenNF
Map=char(NaamFolders(i,1)); % Give me the name of the first , second, ... folder
Pathfiles=[Path,'\',Map,'\','*_new.mat']; %Make the path to the files that end with _new.mat (in my case only 1)
files =struct2cell(dir(Pathfiles)); %Select all files that end with _new.mat (in my case only 1)
NaamFiles=char(files(1,:)); % Make a list of the names of all those files (in my case only 1)
FilePath=[Path,'\',Map,'\',NaamFiles]; %create path to that 1 file
Data=load(FilePath); %load that file
[lenData,lenvariables]=size(Data.ensemble) % determine the size of the file structure called ensemble
idx=NaamFolders{i} %Select the name you want to call your structure in the variable you want to create
index = ismember(Data.ensemble(:,8),[10,20,30]) & Data.ensemble(:,5) == 12; %determine the index where column 5 == 12 and column 8 == 10 or 20 or 30
RightData = Data.ensemble(index,:) % this the data where the previous comment is true
Temperature.idx=RightData(:,9) % this line does not work. For every iteration, create a new structure within temperature that extracts column 9 from RightData.
end
这是最后一行不起作用并显示错误
Unable to perform assignment because dot indexing is not supported for variables of this type.
Error in ExtractTrajectory (line 36)
Temperature.idx=RightData(:,9)
答案 0 :(得分:0)
检查
NaamFolders
格式或为我们打印其格式。
如果以数字开头,您可以做的就是将给定字母添加到所有NaamFolders列表元素中,如下所示
NaamFolders = strcat('z',NaamFolders)
情况1: 以字母
开头>> isvarname("folder1")
ans =
logical
1
可用于点分配
>> check.("folder1") = 5
check =
struct with fields:
folder1: 5
情况2: 以数字
开头
>> isvarname("1folder")
ans =
logical
0
不能与点分配一起使用
>> check.("1folder") = 5
Invalid field name: '1folder'.
情况3: 从空格
开始>> isvarname(" folder")
ans =
logical
0
不能与点分配一起使用
>> check.(" folder") = 5
Invalid field name: ' folder'.
情况4: 以特殊字符
开头>> isvarname("#folder")
ans =
logical
0
不能与点分配一起使用
>> check.("#folder") = 5
Invalid field name: '#folder'.