Variable not being saved/stored from function

30 views (last 30 days)
I created a function to find the dominant frequencies (X acceleration & Z acceleration) from sections of IMU data. The Issue I am having is that my function (or matlab itself) does not seem to save/store the variable XWI.
I discovered this when running different trial data sets through the function. The first few displayed the expected/correct frequencies, meaning they were inphase while running and out of phase while walking. After running more trials through the function, I realized this was not always the case and tried to see where the problem was.
Background: The purpose of this is to eventually determine if the subject is walking or running based on frequncies of the X and Z axis. Based Biomechanics, forward kinetic energy and gravitational potential energy are out of phase while walking (different frequencies of X and Z axis) and in phase while running (same frequncy of X and Z axis) .
Data: The data is from a 3-axis IMU. Sampled at 100Hz for 30 seconds (totaling 3000 data points for each axis). The first ~10 seconds (~ data points 0-1000 ) are the subject walking, the next ~10 seconds (10-20secs, datapoints 1001-2000) are the subject running, the final ~10 seconds (20-30secs, datapoints 2001-3000) are the subject walking again.
Steps I've taken to narrow down the issue:
  • First: I thought it was an issue with the noise/accuracy of Data, so I plotted the magnitude spectrum to see if the frequencies the function outputted were correct. The magnitude spectra showed that the real/true dominant frequency of the X-axis during walking was not the outputted frequency from the function (output --> XWF).
  • Second: I thought there might be an error in finding the max peak of the magnitude spectrum. I inspected the peaks array (XWpks) to see if the max peak (XWM) was detected correctly. It was detected correctly
  • Third: I checked to see if the index of the max peak (XWM) corresponded to the index of the correct (real) frequency in the location array (XWlocs). This was found to be true (index of max peak = index of correct frequncy location). However, the reported index (XWI) of the max peak (XWM) was no where to be found in the workspace, XWI does not exist even when I try to call it in the command window.
  • Finally, this missing variable (XWI) seems to cause the issue of displaying/outputing the wrong frequncy for walking data because function doesn't call the correct indexing of the frequency array ----- I have tried changing the variable name and establishing the variable in the code before, but these do not work. It is worth noting that the "missing" variable (XWI) does not appear even for those datasets that display the correct frequncies.
Here is my function...
function [ZWF,XWF,ZRF,XRF] = walk_run(n)
Fs = 100;
Ts = 1/Fs;
N = 1000; % Length of signal
t = (0:N-1)*Ts; % Time vector
ZrawWalk = n(1:1000,3); % column 3 is Z-axis data
XrawWalk = n(1:1000,1); % column 3 is X-axis data
ZrawRun = n(1001:2000,3);
XrawRun = n(1001:2000,1);
ZfiltWalk = filter(butterworth490filter, ZrawWalk);
XfiltWalk = filter(butterworth490filter, XrawWalk);
ZfiltRun = filter(butterworth490filter, ZrawRun);
XfiltRun = filter(butterworth490filter, XrawRun);
ZfreqWalk = fft(ZfiltWalk);
XfreqWalk = fft(XfiltWalk);
ZfreqRun = fft(ZfiltRun);
XfreqRun = fft(XfiltRun);
N2 = N/2; % Valid spectral points
ZmagWalk = abs(ZfreqWalk); % Compute magnitude spectrum
XmagWalk = abs(XfreqWalk); % Compute magnitude spectrum
ZmagRun = abs(ZfreqRun);
XmagRun = abs(XfreqRun);
f = (0:N-1)*Fs/N; % Frequency vector
[ZWpks,ZWlocs] = findpeaks(ZmagWalk(1:N2),f(1:N2));
[XWpks,XWlocs] = findpeaks(XmagWalk(1:N2),f(1:N2));
[ZRpks,ZRlocs] = findpeaks(ZmagRun(1:N2),f(1:N2));
[XRpks,XRlocs] = findpeaks(XmagRun(1:N2),f(1:N2));
[ZWM,ZWI] = max(ZWpks);
ZWF = ZWlocs(1,ZWI);
[XWM,XWI] = max(XWpks);
XWF = XWlocs(1,XWI);
[ZRM,ZRI] = max(ZRpks);
ZRF = ZRlocs(1,ZRI);
[XRM,XRI] = max(XRpks);
XRF = XRlocs(1,XRI);
end
here is calling the function with 2 different data sets (lefttrain1 is a set that does not display the correct walking X-axis frequency, lefttrain41 is a set that displays the correct walking X-axis frequency)
clc
[ZWF,XWF,ZRF,XRF] = walk_run(lefttrain1)
output:
ZWF = 1.6000
XWF = 1.6000
ZRF = 2.4000
XRF = 2.4000
clc
[ZWF,XWF,ZRF,XRF] = walk_run(lefttrain41)
output:
ZWF = 1.6000
XWF = 0.8000
ZRF = 2.4000
XRF = 2.4000

Answers (1)

Steven Lord
Steven Lord on 3 Dec 2020
function [ZWF,XWF,ZRF,XRF] = walk_run(n)
Your function declaration does not list XWI as a variable that it returns to its caller. Therefore, when the walk_run function exits and its workspace is destroyed, XWI is destroyed along with it. The data that was assigned to ZWF, XWF, ZRF, and XRF inside walk_run may be returned to the caller (if walk_run was called with the appropriate number of output arguments) and stored in the variables or sections of variables with which walk_run was called.
[apple, BMW, Connecticut] = walk_run(42)
That would return whatever was stored in ZWF inside walk_run to be stored in the variable apple in the workspace in which walk_run was called, XWF in BWM, and ZRF in Connecticut.

Community Treasure Hunt

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

Start Hunting!