My datastructure only contains the last subjects information. So in my for loop the structure is getting overwritten. How can I change this?

2 次查看(过去 30 天)
Data= struct(); %Defines Datastructure
Subjects = dir('C:\Users\kaatg\OneDrive\Documenten\KULeuven\BREMS\MATLAB\Assignment'); %Looks into the map Assigment for the information
Subjects = struct2table(Subjects); %Put it into a table, so you can do the following things with it
name = Subjects.name; % Takes out the column of the names
isSub= contains(name,'A'); %Logical indexing, only the rows that contain an A are getting a 1 assignt
Sub = Subjects(isSub,1:2); %Take the first and second colum of the subjects that contain an A
%%
for i= 1:size(Sub,1) %For every subject in the table Sub they will run this loop
Path = string(Sub.folder(i)); %The path to get to the folder of current the subject
DatumFolder = struct2table(dir(Path+filesep+string(Sub.name(i)))); %Look into the folder of the current subject
Datum = DatumFolder.name; %Takes the row names name out of the table datumfolder
isDatum = contains(Datum,'2018'); %Logical indexing, only the rows that contain 2018 are getting a 1 assignt
Folder = DatumFolder(isDatum,1:2); %Take the first and second column of the datumfolder that contain 2018
Folder2 = string(Folder.folder);%Takes the folder columns of the Folder table
SubjectName = Sub.name(i);%Takes the current subjects name
Path2 = struct2table(dir(Folder2+filesep+string(Folder.name)));%Creates a path, looking into the datum folder
Path2 = string(Path2.folder(1)); %Takes only 1 row of the folders column to create a path
Datastructure = Input_Data(Data, Path2, SubjectName); % uses the function made, to create a structure
end

回答(1 个)

Rik
Rik 2023-12-6
Your last line overwrite the struct every iteration. Using indexing will create an array:
Datastructure(i) = Input_Data( ...
  2 个评论
Kaat
Kaat 2023-12-6
If I do that it gives me an error: Error in untitled (line 20)
Datastructure(i) = Input_Data(Datastructure, Path2(i), SubjectName(i))
Rik
Rik 2023-12-6
You didn't copy the entire error.
In this case the error is because in the original code you put Data and in the edit you put Datastructure, so suddenly it becomes self referential.
The exact edit required depends on what you actually want to have as a data structure after your loop.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by