Effacer les filtres
Effacer les filtres

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

11 vues (au cours des 30 derniers jours)
Sean Byrne
Sean Byrne le 30 Jan 2021
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]

Réponses (1)

Iuliu Ardelean
Iuliu Ardelean le 30 Jan 2021
Modifié(e) : Iuliu Ardelean le 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) = []

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by