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

2 views (last 30 days)
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 =; % 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(; %Look into the folder of the current subject
Datum =; %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 =;%Takes the current subjects name
Path2 = struct2table(dir(Folder2+filesep+string(;%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

Answers (1)

Rik on 6 Dec 2023
Your last line overwrite the struct every iteration. Using indexing will create an array:
Datastructure(i) = Input_Data( ...
Kaat on 6 Dec 2023
If I do that it gives me an error: Error in untitled (line 20)
Datastructure(i) = Input_Data(Datastructure, Path2(i), SubjectName(i))
Rik on 6 Dec 2023
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.

Sign in to comment.


Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!