Find indices in a structure array by comparing the name
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Gerhard Weiss
am 1 Dez. 2020
Kommentiert: Ameer Hamza
am 2 Dez. 2020
I have struct array that is similar like that:
Block.name= {'Time','Signal_x','Signal_y','Signal_z'};
Block.channel(1).values= 1:10; %time vetor for all signals in this struct
Block.channel(2).values= rand(1,10); %correspoinding values of Signal_x
Block.channel(3).values= rand(1,10); %correspoinding values of Signal_y
Block.channel(4).values= rand(1,10); %correspoinding values of Signal_z
Now I want to pick out the values of a certain signal, assuming I know the name of the signal but I don't know the order of channels.
Name_desiredSignal= 'Signal_y'; %e.g. values of the Signal_y
I know I can do this by a loop like this:
for idx=2:length(Block.name) %I know that the first channel is always the time vector
if(strcmp(Block.name(idx),Name_desiredSignal))
channel_Nr= idx;
end
Once I have the channel number/index, I can easily get it:
desiredSignal= Block.channel(channel_Nr).values;
I wonder if there is a smarter way, which avoids the loop?
Thanks for help!
0 Kommentare
Akzeptierte Antwort
Ameer Hamza
am 1 Dez. 2020
That for-loop is same as
channel_Nr = find(contains(Block.name, Name_desiredSignal));
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differential Equations 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!