I have emg signal (30000x4) and want to apply function on it in loop....
function waveLen = find_waveform_length(signal)
sum=0;
for i=2:length(signal)
sum= sum + abs(signal(i)-signal(i-1));
end
waveLen = sum;
%%%%%% New file %%%%%%%
load ('dath001.mat');
x = dath001; % 30000x4
for i = 1:4
waveLen(i) = find_waveform_length(x(i))
end
but it give output of 0 0 0 0
instead if i run waveLen function separately it give output of
463.6428 200.0671 411.7511 372.9306 ... I want this output by simple apply loop command
Thank you

 採用された回答

Raj
Raj 2019 年 9 月 18 日

0 投票

What is the 'y' you have used in your main script loop? Its not defined anywhere. Use this line in main script:
waveLen(i) = find_waveform_length(x(:,i))
Since you have not shared your MAT file, I tried with a random 30000x4 matrix and it works.
Few other recommendations:
1) Put an 'end' for your function
2) Don't use variable 'i' or any other inbuilt Matlab variable as index in your 'for' loop.

4 件のコメント

Ali Asghar
Ali Asghar 2019 年 9 月 18 日
Thank you dear
Ali Asghar
Ali Asghar 2019 年 9 月 18 日
Dear
now i want to apply windowing in it....
previously i did in below way
w = hann(2000);
datawindow1 = w.*buffer(y(:,1),2000,-500); % buffer is good command, read it
datawindow2 = w.*buffer(y(:,2),2000,-500);
datawindow3 = w.*buffer(y(:,3),2000,-500);
datawindow4 = w.*buffer(y(:,4),2000,-500);
but when i do it in loop like
for i = 1:4
datawindow(i) = w.*buffer(y(:,i),2000,-500)
end
it shows error
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Raj
Raj 2019 年 9 月 18 日
Again you have not mentioned what is 'y' here.
Lets see your expression in the loop:
datawindow(i) = w.*buffer(y(:,i),2000,-500)
You are trying to dynamically name the variable datawindow. It is not recommended. See this:
Now coming to your error. The right side of this expression will give you a matrix of some size and you are trying to assign that to a single element of matrix datawindow hence you are getting the size mismatch error.
Wayout: Define datawindow as a cell array like this:
datawindow=cell(4,1);
Then in your loop use this expression:
datawindow(i,1) = {w.*buffer(y(:,i),2000,-500)};
Now your datawindow will have 4 matrices as cell elements. You can extract and use them like this:
datawindow1=cell2mat(datawindow(1,1));
and so on.
See these links for details:
Ali Asghar
Ali Asghar 2019 年 9 月 19 日
oky dear

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 9 月 18 日

コメント済み:

2019 年 9 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by