MATLAB Answers

0

How do I plot every 10th row of data?

Asked by Samantha Wang on 9 Aug 2019
Latest activity Edited by SHUBHAM GUPTA on 9 Aug 2019
I am working with files with nearly two million rows of data, and processing everything can take up to 6-7 hours. I'd like to speed things up a bit by taking every 10th row instead. Below is an example of what I'm working with. Data is being used to plot bx, by, bz against time.
2011 083 23 59 57.079 209498665.408 6933.648 7397.546 -4196.500 32.928 -0.989 -1.357
2011 083 23 59 57.129 209498665.458 6933.624 7397.529 -4196.438 32.927 -1.175 -1.798
2011 083 23 59 57.179 209498665.508 6933.601 7397.511 -4196.377 32.880 -1.268 -2.359
2011 083 23 59 57.229 209498665.558 6933.577 7397.494 -4196.316 32.832 -1.427 -2.931
2011 083 23 59 57.279 209498665.608 6933.553 7397.477 -4196.255 32.831 -1.335 -3.392
2011 083 23 59 57.329 209498665.658 6933.529 7397.460 -4196.193 32.878 -1.389 -3.639
2011 083 23 59 57.379 209498665.708 6933.505 7397.443 -4196.132 32.879 -1.534 -3.767
2011 083 23 59 57.429 209498665.758 6933.481 7397.425 -4196.071 32.926 -1.892 -3.863
2011 083 23 59 57.479 209498665.808 6933.457 7397.408 -4196.009 32.879 -2.327 -3.906
2011 083 23 59 57.529 209498665.858 6933.434 7397.391 -4195.948 32.879 -2.842 -3.893
2011 083 23 59 57.579 209498665.908 6933.410 7397.374 -4195.887 32.879 -3.384 -3.920
My current script is this:
finput='C:\Users\must\Downloads\';
foutput='C:\Users\must\Downloads\inout\';
filename='MAGMSOSCI11083_V08.TAB';
fid=fopen([finput filename],'r');
%data
tline=fgetl(fid);
i=1;
while tline(1)=='2'
% num1=str2num(tline(1:4));
% test=num1(1);
% if test>-999&&test<999
year(i,1)=str2num(tline(1:4));
day(i,1)=str2num(tline(6:8));
hour(i,1)=str2num(tline(10:11));
minute(i,1)=str2num(tline(13:14));
second(i,1)=str2num(tline(16:21));
t(i,1)=hour(i,1)+minute(i,1)/60+second(i,1)/3600;
num1=str2num(tline(85:end));
Bx(i,1)=num1(1);
By(i,1)=num1(2);
Bz(i,1)=num1(3);
Bt=sqrt(Bx.^2+By.^2+Bz.^2);
tline=fgetl(fid);
i=i+1;
end
Help would be greatly appreciated!!

  2 Comments

It would be more efficient to textscan the entire file and calculate Bt and plot afterwards.
Rik
on 9 Aug 2019
You mean something like this?
plot(a(1:10:end),b(1:10:end))

Sign in to comment.

1 Answer

Answer by SHUBHAM GUPTA on 9 Aug 2019
Edited by SHUBHAM GUPTA on 9 Aug 2019
 Accepted Answer

One way to do this efficiently would be to scan the data inside the file at once (instead of doing it one by one in a loop) :
fname = [finput filename];
num_vec = textread(fname,'%f');
num_mat = reshape(num_vec,12,length(num_vec)/12)';
year = num_mat(:,1);
day = num_mat(:,2);
hour = num_mat(:,3);
minute = num_mat(:,4);
second = num_mat(:,5);
t = hour + minute/60 + second/3600;
Bx = num_mat(:,10);
By = num_mat(:,11);
Bz = num_mat(:,12);
Bt=sqrt(Bx.^2+By.^2+Bz.^2);
To plot every 10th row :
figure;
grid on;
plot(t(1:10:end),Bt(1:10:end))
I hope it helps!

  0 Comments

Sign in to comment.