MATLAB Answers

Grace
0

How to find the indices of a data column that aren't Nan, and how to use those indices to concatenate a vector?

Asked by Grace
on 27 May 2019
Latest activity Commented on by Grace
on 31 May 2019
My code is using importdata to import the data columns from excel files. I have a bunch of data files that I have a for loop going through and I am trying to make contiuous vectors of all the data from all the files. In the data files, Mx, My, Mz and Temp are somethimes empty and I want to make those vectors without the nans that will appear where there is nothing in the data file.
The second line of the first peice of code I have written is what I have guessed is a way to find the postions where there arent nans and then use that indice in the line that concatenates the Mx vector (I assumed I would have to do this for Mx, My, Mz and Temp individually and I havent written those there), but this function won't produce outputs for each line that isnt a nan, I always get an error that says there are to many output arguments. Is there a way to do this using this function or should I try something else?
elseif size(A.data,2) == 15
[~,i] = not(isnan(A.data(:,12)));
Time = [Time;(A.data(:,1) + mtimeStart*24*60*60)];
Ax = [Ax;A.data(:,2)];
Ay = [Ay;A.data(:,3)];
Az = [Az;A.data(:,4)];
Mx = [Mx;A.data(i,12)];
My = [My;A.data(:,13)];
Mz = [Mz;A.data(:,14)];
Temp = [Temp;A.data(:,15)];
TimeMag = [TimeMag;(A.data(:,1) + mtimeStart*24*60*60)];
TimeTemperature = [TimeTemperature;(A.data(:,1) + mtimeStart*24*60*60)];
%% full code
N = length(fns);
Time=[];
Ax=[];
Ay=[];
Az=[];
Mx=[];
My=[];
Mz=[];
Temp=[];
TimeMag=[];
TimeTemperature=[];
times=[];
temperatureTimes=[];
magTimes=[];
for kk = 1:1:N
fn = fns(kk).name;
A = importdata([dn,fn]);
%find time stamps
start_time = A.textdata(3,1);
str = char(start_time);
S = strfind(str,' '); %finds the spaces
DateandTime = extractAfter(str,S(1));
DT = datetime(DateandTime,'InputFormat','yyyy-MM-dd,HH:mm:ss.SSS');
startTime = timeofday(DT);
mtimeStart = datenum(DT);
if size(A.data,2) == 11
Time = [Time;(A.data(:,1) + mtimeStart*24*60*60)];
Ax = [Ax;A.data(:,2)];
Ay = [Ay;A.data(:,3)];
Az = [Az;A.data(:,4)];
for ii = 1:1:size(A.data,1)
numNan(ii) = sum(isnan(A.data(ii,:)));
% if nans in row then it is slow magnetometer data
if sum(isnan(A.data(ii,:))) == 7 %Mx, My, Mz, Temp
Mx = [Mx;A.data(ii,1)];
My = [My;A.data(ii,2)];
Mz = [Mz;A.data(ii,3)];
Temp = [Temp;A.data(ii,4)];
TimeMag = [TimeMag;(A.data(ii-1,1) + mtimeStart*24*60*60)];
TimeTemperature = [TimeTemperature;(A.data(ii-1,1) + mtimeStart*24*60*60)];
elseif sum(isnan(A.data(ii,:))) == 8 %Mx, My, Mz
Mx = [Mx;A.data(ii,1)];
My = [My;A.data(ii,2)];
Mz = [Mz;A.data(ii,3)];
TimeMag = [TimeMag;(A.data(ii-1,1) + mtimeStart*24*60*60)];
end
end % ii
elseif size(A.data,2) == 15
Time = [Time;(A.data(:,1) + mtimeStart*24*60*60)];
Ax = [Ax;A.data(:,2)];
Ay = [Ay;A.data(:,3)];
Az = [Az;A.data(:,4)];
Mx = [Mx;A.data(:,12)];
My = [My;A.data(:,13)];
Mz = [Mz;A.data(:,14)];
Temp = [Temp;A.data(:,15)];
TimeMag = [TimeMag;(A.data(:,1) + mtimeStart*24*60*60)];
TimeTemperature = [TimeTemperature;(A.data(:,1) + mtimeStart*24*60*60)];
elseif size(A.data,2) == 14
Time = [Time;(A.data(:,1) + mtimeStart*24*60*60)];
Ax = [Ax;A.data(:,2)];
Ay = [Ay;A.data(:,3)];
Az = [Az;A.data(:,4)];
Mx = [Mx;A.data(:,12)];
My = [My;A.data(:,13)];
Mz = [Mz;A.data(:,14)];
TimeMag = [TimeMag;(A.data(:,1) + mtimeStart*24*60*60)];
for ii = 1:1:size(A.data,1)
numNan(ii) = sum(isnan(A.data(ii,:)));
if sum(isnan(A.data(ii,:))) == 13 %T
Temp = [Temp;A.data(ii,1)];
TimeTemperature = [TimeTemperature;(A.data(ii-1,1) + mtimeStart*24*60*60)];
end
end % ii
else
%display error message
disp(['Uh-oh ',num2str(kk),' ', fn])
end %dealing with width differences in data
end %kk

  0 Comments

Sign in to comment.

1 Answer

Answer by Rik
on 27 May 2019
 Accepted Answer

The not function (or simply ~) doesn't have a second output. Luckily you don't need it. You can directly use its output to perform a logical indexing operation.
You might want to combine each of your 3 variables filter list with or (use a single pipe |) to keep all variables aligned.

  2 Comments

Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
Thank you! Using what I named the ~isnan function to indicate the row worked!

Sign in to comment.