Azzera filtri
Azzera filtri

How can I Plot continuation plot?

5 visualizzazioni (ultimi 30 giorni)
Omer
Omer il 25 Giu 2014
Commentato: Omer il 29 Giu 2014
Hi there,
I have multiple files and each file contain data (in the form of multiple columns). I am trying to plot a single column of each file as a continuation. However, what I am getting is superimposed (single column of one file plotted on the top of other file, like a hold all/hold on plot).
Suppose, in file 1 we have 500 data points of voltage measurements. In file 2, 3, 4, 5, 6 and 7, we have 400 data points of voltage measurments. So how can I plot a graph of file 1 of 500 data points starting from 0 - 500, and then file 2 501 - 900, and so on.
One way of doing is concatination. Can someone explain how concatination works inside for loop as I tried that option too but it didnt work for me.

Risposta accettata

Omer
Omer il 27 Giu 2014
Sorry dpb I am not using csvread to read the file as I have a directory of thousands of files and therefore i am using following code to read them. I only want to plot 1st column of the each file something like Plot(Data(:,1)) and I dont wanna use other columns. Please check the code below for me let me know what error I am making or how can I fix it
close all
clear all
clc
loadingDir = '/Users/OP/Desktop/Data/DataChar/TestType/Voltage';
processedFiles = dir(fullfile(loadingDir,'*.csv'));
numOfFiles=size(processedFiles);
for k= numOfFiles :-1:1
y=processedFiles(k).name
temp1=importdata(fullfile(loadingDir,y));
Data=temp1.data;
lenTot = length(Data(:,1));
hL= plot(Data(:,1))
hold all % using it to see whether it is adding?
for i=2:length(k) % loop over the rest
y=processedFiles(k(i).name);
lenTot=lenTot+length(y); % increment cumulative length
set(gca,'xdata',[1:lenTot],'ydata',[get(hL,'ydata').' y(Data(:,1)]);
end
end
Regards
  1 Commento
dpb
dpb il 27 Giu 2014
Modificato: dpb il 27 Giu 2014
Don't put additional comments, etc., in Answer section; either use Comments or edit the original Question...
Here's my original with the substitution of importdata for textread -- you'll have to double check for typos, etc,... it's just typed in at the screen.
Again, the idea is to get the directory structure, plot the first outside the loop, then loop over the remainder from the 2nd on, adding them to the plot data already have.
You may need to update xlim as well, I'm not positive w/o testing whether the hold on will keep the x axes from autoranging or not--I kinda' think they'll expand automagically but you may need to reset the xlim to the range over [1 lenTot] if they don't...
path = '/Users/OP/Desktop/Data/DataChar/TestType/Voltage';
d = dir(fullfile(loadingDir,'*.csv')); % i went to the shorter names
y=importdata(d(1).name); % read first if insist on importdata
lenTot=y.data; % length of first
hL=plot(y.data(:,1)); % 1st plot IDX col vs position index
hold on % for subsequent to plot on axes
for i=2:length(d) % loop over the rest from 2nd on...
y=importdata(d(i).name);
lenTot=lenTot+length(y.data); % increment cumulative length
set(gca,'xdata',[1:lenTot], ...
'ydata',[get(hL,'ydata').' y.data(:,1)]); % add current to plot
end
ADDENDUM
As a check on the last point, by using set to update the x|ydata properties you don't need hold on at all...here's a simple demo of the idea at the command line; all you've got to do is read your files and substitute your column of data for the random vector I used here (and the correct length, of course, instead of the hardcoded 10)--
hl=plot(rand(10,1)); % open the figure; get the line handle
L=10; % using constant length here; find for the real data
for i=1:10
L=L+10; % increment length for x vector
y=[get(hl,'ydata').'; rand(10,1)]; % add a new sample to existing
set(hl,'xdata',[1:L],'ydata',y) % and update plot
pause(0.2) % just a little delay to observe
end
Note the x-axis limits will update automagically to show the full width...

Accedi per commentare.

Più risposte (2)

dpb
dpb il 25 Giu 2014
If all you want is the plot, no need to concatenate, simply keep the cumulative length as you go and continue on the plotting position from there...sotoo (caution, aircode, not tested)...
d=dir('*.dat'); % presume the files to process
y=textread(d(1).name); % read first
lenTot=length(x); % length of first
hL=plot(y(idx)) % 1st plot IDX col vs position index 1:lenTot, save handle
hold on
for i=2:length(d) % loop over the rest
y=textread(d(i).name);
lenTot=lenTot+length(y); % increment cumulative length
set(gca,'xdata',[1:lenTot], ...
'ydata',[get(hL,'ydata').' y(idx)]); % add current data to plot
end
If you do need the full dataset in memory to do other computations on, then basically follow same idea except preallocate an array estimated be roughly as big as the total when done and fill it in the loop using the same idea of determining the present total length to know that the next insertion point in the array is lenTot+1.
Do not start with the empty array and let lazy reallocation handle it; for any size file at all this will cause massive slowdown in practice. That's the case of
y=textread(d(1).name;
for i=2:length(d)
y=[y;textread(d(i).name)];
...
This is easy to write but very bad on memory reallocation and data recopying. For tiny problems it'll not be terribly noticeable but...
  8 Commenti
dpb
dpb il 29 Giu 2014
for i=2:length(d)
y = importdata (fullfile(loadingDir,d(i).name));
lenTot=lenTot+length(y.data(:,1));
set(hL,'xdata',[1:lenTot],'ydata',y)
What you've not done, however, in comparison to the example I showed is to append the new set of data to that already plotted -- the above will give you a length mismatch because the x-vector is growing but the y-vector isn't; you're replacing the existing but not appending it.
Refer back to my example which was written as--
y=[get(hl,'ydata').'; rand(10,1)]; % add a new sample to existing
set(hl,'xdata',[1:L],'ydata',y) % and update plot
NB: the get() call that obtains the previous plot y-data and appends the new onto it. You'll find it less confusing if you don't use y for two purposes here--I rewrote it with a separate variable y in the demo because I thought it might be simpler for you to see what was happening explicitly rather than hiding that in the set.
newy = importdata (fullfile(loadingDir,d(i).name)); % the added data
set(hl,'xdata',[1:L],'ydata',[y newy.data(:,1)) % update the plot
Omer
Omer il 29 Giu 2014
I had noticed that after debugging that my plot was empty which meant that yvector wasn't growing so instead of using y I replaced it with other variable such as
u=[get(h1,'ydata').';y.data(:,1)]
and then updated the plot as you indicated in your example
set(hl,'xdata',[1:lenTot],'ydata',u)
As a result I was able to get the final plot. Thanks for your help dpb much appreciated.
Regards

Accedi per commentare.


Omer
Omer il 26 Giu 2014
Thanks very much dpb for your answer. However, I am new to matlab so I have very limited understanding how it works. Can you explain it to me with respect to CSV or excel files.
Regards
  1 Commento
dpb
dpb il 26 Giu 2014
doc csvread|write
doc dlmread|write
doc xlsread|write
Simply use the one most appropriate to your particular situation in the outline of above...

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by