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

조회 수: 1 (최근 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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by