Plotting different sections of multiple arrays using a loop

조회 수: 5 (최근 30일)
Bill
Bill 2014년 10월 9일
댓글: Iain 2014년 10월 9일
Hi,
I have multiple data files that I have extracted using a for loop, I can easily plot these files in the loop aswel.
The problem however is that for each file I only want to plot a snapshot of the data. does anyone know if this is possible to introduce in a loop.
My code to plot all the graphs is:
Time=linspace(1,3000,3000/(50*10^(-6)));
Files = dir(fullfile('C:blah blah/*.mat'));
filename = cell(length(Files),1);
data = cell(length(Files),1);
for k = 1:length(Files)
filename{k} = Files(k).name;
data{k} = load(filename{k});
figure;
plot(Time(1:length(data{k,1})),data{k,1});
title(filename(k))
end
but to reiterate i want to say plot first data points 1000-2000 for 1, 1-1001 for 2 etc the only similarity is that the data ranges I want to plot are the same. Is this possible?

답변 (1개)

Iain
Iain 2014년 10월 9일
편집: Iain 2014년 10월 9일
Assuming your plot command is correct, all you need to do is modify it:
plot(Time(1:length(data{k,1})),data{k,1});
plot(Time(1:1000),data{k,1}(1:1000));
  댓글 수: 2
Bill
Bill 2014년 10월 9일
yeah thanks but what happens if I want to plot say 1:1000 for k=1, but 501-1500 for k=2, 201-1200 for k=3 etc is this possible in a loop? Or should is just plot them all individually
Iain
Iain 2014년 10월 9일
You just need a function that tells you the first index to use, and a function that tells you the last index to use.
something like
first = [1 501 201 640 981 321];
first_index = first(k);
plot(Time(first_index:(first_index+999)),data{k,1}(first_index:(first_index+999)));
I am assuming that you want the same part of "Time" as you do data...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by