How to add a value to the next available column or row in an array using for loop?

20 views (last 30 days)
I am using a for loop to read in multiple points of data and want to combine into a single array.
See below for a simplified version of the code im working with.
for i = 1:5
filename = strcat(path,filenames{i});
genericdata = importdata(filename);
Data.(genericdata.Task(1,:)).Var1(:,i) = genericdata.Var1;
end
This is fine when the value at generic.Task(1,:) remains the same, however this changes depending on the file imported.
As such I end up with values looking like
Data.A.Var1 = [1 1 0 0 0]
Data.B.Var1 = [0 0 1 0 1]
Data.C.Var1 = [0 0 0 1 0]
Where what I need is as below
Data.A.Var1 = [1 1]
Data.B.Var1 = [1 1]
Data.C.Var1 = [1]

Answers (1)

Iuliu Ardelean
Iuliu Ardelean on 30 Jan 2021
Edited: Iuliu Ardelean on 30 Jan 2021
You can try removing whatever elements are equal to zero:
Data.A.Var1(Data.A.Var1 == 0) = []
Data.B.Var1(Data.B.Var1 == 0) = []
Data.C.Var1(Data.C.Var1 == 0) = []

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!