Create cell with vector from looping trough struct

11 次查看(过去 30 天)
Hello. I have a struct, 1x30, each field has a 1*X columns with index values. some fields are also empty. Im trying to greate a vector, which goes from each index value + 100index points. Then save this as a cell containing a vector ranging from indexvalue to indexvalue +100. for all columns within the 30 fields. Looking at the picture below, indexstruct.col_5, i need a vector going from
149111, 149112,149113....149211
385351, 385352, 385353,.....385451
and so on for all 168 columns in indexstruct.col_5 but also for all columns in col_1 to col_30.
The vectors are then to be used to findi maxvalue within each index-range, ranging from start to 100 indexpoints later. indexvalues correspond to time in an acceleration time history.
Gratefull for any help i can get!
For what its worth im posting my code below. Although I belive its pretty far from achiving what i want.
vec2={}
fn = fieldnames(indexstruct);
for k=1:numel(fn) %looping through struct
for i = length(indexstruct.(fn{k}))
vec2 = (indexstruct.(fn{k})(:,i)):indexstruct.(fn{k})(:,i)+100;
vec2= {vec2;vec2(k)};
end
end
  2 个评论
Stephen23
Stephen23 2020-5-16
Using numbered fields makes accessing your data more complex than it needs to be. Much simpler:
  • a non-scalar structure
  • a cell array.
Both of these would make accessing your data easier.
Tor Aarskog
Tor Aarskog 2020-5-22
Hi, Thanks for you advice, do you have any further specific advice on how i could alter the code ?
x=Veracity6raw.y(:,:); % 3686400x30 matrix
indexstruct=struct;
indexvec=[];
j=1;
index=1;
runloop=true;
i=0;
while runloop
i=i+1;
if index+100>length(x)
index=1;
ind_col=['col_' num2str(j)];%make new struct field name
indexstruct.(ind_col)=indexvec;% Make new field in struct for indexvec with column number
indexvec=[]; %make indexvec empty for new column
j=j+1;
if j>size(x,2)%If j is larger than numbers of columns, break loop
break
end
end
vec1=x(index:index+100,j);
if std(vec1)>std_tol(j)
indexvec=[indexvec,index];
index=index+100;
else
index=index+10;
end
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by