How to apply several .csv files to matlab one plot
Mostrar comentarios más antiguos
Hello guys,
I want to apply 30 .cvs files into one matlab plot. I am attaching the original code which I am using to read only one file.
filename = 'jan1.csv';
elapsedTime = xlsread(filename, 'F:F');
elapsedTime = elapsedTime - elapsedTime(1);
deltaX = xlsread(filename, 'C:C');
deltaXmean = mean(deltaX);
deltaX = deltaX - deltaXmean;
deltaY = xlsread(filename, 'D:D');
deltaYmean = mean(deltaY);
deltaY = deltaY - deltaYmean;
deltaZ = xlsread(filename, 'E:E');
deltaZmean = mean(deltaZ);
deltaZ = deltaZ - deltaZmean;
So, i have about 30 .csv files named as "jan1" "jan2" etc. What I want to do is data from all of the files to be shown in the one plot. The file type is the same, means that the columns are in the same order for all the files.
Thanks a lot!
Respuestas (1)
Walter Roberson
el 13 de Jul. de 2016
0 votos
6 comentarios
Azar Guliyev
el 13 de Jul. de 2016
Walter Roberson
el 13 de Jul. de 2016
dinfo = dir('jan*.csv');
filenames = {dinfo.name};
filenames = sort_nat(filenames); %using the routine from the file exchange
num_files = length(filenames);
for K = 1 : num_files
filename = filenames{K};
[~, basename, ~] = fileparts(filename);
elapsedTime = xlsread(filename, 'F:F');
elapsedTime = elapsedTime - elapsedTime(1);
deltaX = xlsread(filename, 'C:C');
deltaXmean = mean(deltaX);
deltaX = deltaX - deltaXmean;
deltaY = xlsread(filename, 'D:D');
deltaYmean = mean(deltaY);
deltaY = deltaY - deltaYmean;
deltaZ = xlsread(filename, 'E:E');
deltaZmean = mean(deltaZ);
deltaZ = deltaZ - deltaZmean;
h(:,K) = plot3(deltaX, deltaY, deltaZ); %you didn't say anything about how you wanted them plotted
hold on
legends(1:size(h,1), K) = {basename};
end
legend( h(:), legends(:) );
This code takes into account the possibility that you might be generating multiple lines for each file, each of which is to be given the same legend entry with the legend entry being the file name such as 'jan14' . It does, however, in its current form assume that you are generating the same number of lines for every file. You did not give a sample graph so I assumed that you want to plot the delta values against each other, for lack of anything better.
Azar Guliyev
el 13 de Jul. de 2016
Walter Roberson
el 13 de Jul. de 2016
Did you download and install the File Exchange contribution that I posted the link to? https://www.mathworks.com/matlabcentral/fileexchange/10959-sort-nat--natural-order-sort
To plot the three against elapsed_time:
h(:,K) = plot(elapsed_time, deltaX, elapsed_time, deltaY, elapsed_time, deltaZ);
With 3 lines plotted for each .csv and 30 .csv files, that is going to make 90 lines on the same graph. Even if you use different line styles for deltaX compared to deltaY or deltaZ, you are going to have a very difficult time creating 90 distinct lines. The plt() File Exchange contribution can help for that, but the limit for it is around 48 distinct lines before the potential combinations give out.
Are you sure you want all 90 lines on the same plot? And not just 3 lines per plot, with 30 different plots being created? (But if so, what do you want done with the 30 plots?)
will it give me an error at the end if the quantity of arrays in each of the excel files are not the same
If you do not have enough columns to reach column F in each of the files then you are going to have a problem. However, the above code will not have a problem if there are a different number of rows in each file.
Azar Guliyev
el 14 de Jul. de 2016
Editada: Azar Guliyev
el 14 de Jul. de 2016
Walter Roberson
el 14 de Jul. de 2016
Download the .zip into a handy directory that is not under the MATLAB installation directories. Unzip it, which will create a directory there. Now go into MATLAB and use the pathtool command to add that directory to the MATLAB path and save the path. You will now be able to use the routine.
Categorías
Más información sobre Entering Commands 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!