Help with looping a function
Show older comments
Hello, I'm newbie to Matlab (coding in general) and I am having difficulty with running a loop. I have a function that was previously given to me (below) that was made to take an xy plot (Time and position/velocity/acceleration) and using the plot and a certain threshold y value find an x value (specific timepoint). I am now trying to do something similar, however, this threshold value (level) is actually an array of timepoints that I am fishing for in my position time data. The problem is that when I try and run the function I am met with the error that the arrays have incompatible sizes. It seems that the level can only be a single value everytime it is ran. So I thought that a loop would be helpful as ideally each time it ran the level would be equal to the next value in the FootStart_time array. In the initial loop below this just recreated the problem as the level was equal to the entire array rather than a single element. I also tried numel and the text in green, but I just kept running into the problem of not being able to have i equal a single value in the FootStart_tRF and then have it loop. Any ideas or pointers on what I'm missing would be appreciated.
for i=(FootStart_tRF) %also tried i=1:(FootStart_tRF) but this didn't work either%
level=i %how to make level correspond to individual values of FootStart_tRF?%
FootStart_xRF=FindTime(RFXS,Time, level)
end
Function:
function FootStart_xRF = FindTime(RFXS,Time,level)
cxi = find(diff(sign(Time-level)));
for k = 1:numel(cxi)
idxrng = max(1,cxi(k)-1) : min(numel(RFXS), cxi(k)+1);
FootStart_xRF(k) = interp1(Time(idxrng), RFXS(idxrng), level);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!