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

12 Ansichten (letzte 30 Tage)
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]

Antworten (1)

Iuliu Ardelean
Iuliu Ardelean am 30 Jan. 2021
Bearbeitet: Iuliu Ardelean am 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) = []

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by