how to delete nan values from array columns without losing data from other columns
Show older comments


i have a huge file from a lab expremient
every run gives me 4 columns and every 2 out of the 4 start with nan then 2 nan vars and end with nan and 3 nan vars i want to delet the nan values without losing any data so that when i try to make graphs from it every graph will have enough inputs and neither of them get lost
i am espisally troubled by the second 2 columns of every 4 and how to deal with them
11 Comments
dareen
on 13 May 2024
Rik
on 13 May 2024
What code are you using to plot?
The problem is that Matlab does not allow holes in arrays, so you can't remove those NaN values without removing a row/column. You can get close to the effect with a cell array (which can contain an empty array), but most plotting functions can't handle cell inputs.
Mathieu NOE
on 13 May 2024
we could probably be more efficient if you would also share the data
also I am not sure how v(t) is used (or not ) in your equation x(t)=a(t) sin(w*t+phi) or what is it for ?
ok, so you want to fit a exponential decaying sine wave to your data ... that was not very clear at the beginning
first I wanted to have a look at your data , at you have many (34) runs and I was not sure what was in your data
now , which runs are the best to use ? probably not the first ones
data= readmatrix('expr2lab.csv'); % or readtable or whatever
runs = size(data,2)/4;
for ck = 1:runs
ind_cols = (1:4)+(ck-1)*4; % Time (s) Position (m) Velocity (m/s) Acceleration (m/s²)
data_this_run = data(:,ind_cols);
[m,n] = size(data_this_run);
Time = data_this_run(:,1);
Position = data_this_run(:,2);
Velocity = data_this_run(:,3);
Acceleration = data_this_run(:,4);
figure(ck)
subplot(3,1,1),plot(Time,Position)
title(['Run # : ' num2str(ck)]);
ylabel('Position');
subplot(3,1,2),plot(Time,Velocity)
ylabel('Velocity');
subplot(3,1,3),plot(Time,Acceleration)
ylabel('Acceleration');
end
Mathieu NOE
on 14 May 2024
glad I could help a bit , but this was just a starter
ok, let's focus on the selected runs (for both files I presume ?)
these 2 lines are indeed just to extract a block of 4 contiguous columns , and we simply shift by a factor 4 when we change to the next run :
ind_cols = (1:4)+(ck-1)*4;
data_this_run = data(:,ind_cols);
and yes this line is not anymore of any use (I used it for another purpose that I removed afterwards) : [m,n] = size(data_this_run);
I'll come back soon with a new code
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification 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!














































