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

16 visualizaciones (últimos 30 días)
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]

Respuestas (1)

Iuliu Ardelean
Iuliu Ardelean el 30 de En. de 2021
Editada: Iuliu Ardelean el 30 de En. de 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) = []

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by