Azzera filtri
Azzera filtri

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

10 visualizzazioni (ultimi 30 giorni)
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]

Risposte (1)

Iuliu Ardelean
Iuliu Ardelean il 30 Gen 2021
Modificato: Iuliu Ardelean il 30 Gen 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) = []

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by