Create cell with vector from looping trough struct

9 ビュー (過去 30 日間)
Tor Aarskog
Tor Aarskog 2020 年 5 月 16 日
コメント済み: Tor Aarskog 2020 年 5 月 22 日
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 Center および File ExchangeTables についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by