Help with looping a function

8 views (last 30 days)
T-800
T-800 on 4 Aug 2022
Commented: Star Strider on 4 Aug 2022
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

T-800
T-800 on 4 Aug 2022
Edited: T-800 on 4 Aug 2022
I figured it out and thought I would put my solution here incase anyone else has the same question.
FootStart_tRF
for i=1:numel(FootStart_tRF)
level=FootStart_tRF(i)
FootStart_xRF=FindTime(RFXS,Time, level)
end
Although this overwrites the FootStart_tRF variable with each loop.
Here's a version that puts all the values in one array (Retotal)
FootStart_tRF
for i=1:numel(FootStart_tRF)
level=FootStart_tRF(i)
FootStart_xRF=FindTime(RFXS,Time, level)
Total(i)=FootStart_xRF
Retotal=Total.'
end
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
  1 Comment
Star Strider
Star Strider on 4 Aug 2022
A slightly more efficient version that produces ‘Retotal’ as a column vector —
for i=1:numel(FootStart_tRF)
FootStart_xRF=FindTime(RFXS,Time, FootStart_tRF(i))
Retotal(i,:)=FootStart_xRF
end
To convert ‘FindTime’ to accept a vector as the ‘level’ argument —
RFXS = linspace(0, 20, 500);
Time = sin(2*pi*RFXS*0.25) + RFXS/15;
levelv = [0.5, 1, 1.5];
FootStart_xRF = FindTime(RFXS,Time,levelv)
FootStart_xRF = 3×11
0.3179 1.7494 4.1437 5.9334 7.9796 10.1114 11.8142 14.2996 15.6350 18.5259 19.4152 0.7927 1.2623 4.4941 5.5669 8.2950 9.7734 12.1229 13.9556 15.9593 18.1341 19.7929 8.7395 9.3159 12.4665 13.5952 16.2726 17.7970 0 0 0 0 0
figure
plot(RFXS, Time)
hold on
for k = 1:size(FootStart_xRF,1)
plot(FootStart_xRF(k,:), zeros(size(FootStart_xRF))+levelv(k), 's', 'MarkerSize',7.5)
end
hold off
grid
function FootStart_xRF = FindTime(RFXS,Time,level)
for k1 = 1:numel(level)
cxi = find(diff(sign(Time-level(k1))));
for k2 = 1:numel(cxi)
idxrng = max(1,cxi(k2)-1) : min(numel(RFXS), cxi(k2)+1);
FootStart_xRF(k1,k2) = interp1(Time(idxrng), RFXS(idxrng), level(k1));
end
end
end
This requires only one call to the function, however a loop to plot the rows is still necessary.
To get column vectors for the output, reverse the indices of the output matrix in the function:
FootStart_xRF(k2,k1) = interp1(Time(idxrng), RFXS(idxrng), level(k1));
.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!