How can I Plot continuation plot?
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (2)
dpb
el 25 de Jun. de 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 comentarios
Image Analyst
el 28 de Jun. de 2014
Omer's "Answer" moved here since it's not an answer to the original question yet he posted it as an Answer despite specifically being asked to post responses as comments and not as new answers:
The problem I am having now is with set when I use the following code.
close all
clear all
clc
loadingDir = '/Users/OP/Desktop/Data/DataChar/TestType/Voltage';
d = dir(fullfile(loadingDir,'*.csv'));
y=importdata (fullfile(loadingDir,d(1).name));
hL=plot(y.data(:,1));
lenTot=length(y.data(:,1));
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)
end
The error I am getting is that conversion to double from struct is not possible which I guess is graphics error.
Omer
el 28 de Jun. de 2014
dpb
el 28 de Jun. de 2014
OK, so have you worked out how to load the data into and address the data vector yet?
dpb
el 29 de Jun. de 2014
That's because y is the data structure returned from importdata; the actual data is in the .data field. You've got to use the structure field name to access the data (the extra unneeded effort of which for such a simple data structure is one of the reasons I kept recommending using one of the other input functions, but you seemed adamant on the point) as y.data
I don't recall; is the actual data you which to plot the first or another column in the file? -- whichever it is, use the proper column number where I've used '1'.
set(hL,'xdata',[1:lenTot],'ydata',y.data(:,1))
Omer
el 29 de Jun. de 2014
dpb
el 29 de Jun. de 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
el 29 de Jun. de 2014
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!