How do I get MATLAB to use first set of data from csv Excel sheet I have?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nathan Nguyen
le 16 Nov 2019
Commenté : Nathan Nguyen
le 17 Nov 2019
Hello,
I'm doing some practice in MATLAB importing data from an Excel csv file. I did an example where I created two vectors for time and population density and then I plotted them. I tried doing the same exact thing, but rather than creating the vectors by hand e.g., time = [2000:1:2016]; population = [blah, blah, .... blah]; I have them as colomns of cells in Excel. I imported the csv file into MATLAB by doing filename = 'P_D.csv'; data = csvread('P_D.csv', INSERT SOMETHING HERE !?). The !? is where I'm stuck. When I plot the data, and compared the graphs, I noticed that the Excel method was messed up. MATLAB is skipping one set of data points i.e., rather than starting at Time = 2000 and population density = some #, MATLAB is doing Time = 2001, and population density = some # attached to 2001. I don't want to skip the year 2000's data points. How do I fix this? I'll include the M-file and excel file for you guys.
Thank You!
Time = [2000:1:2016];
t = Time;
Population_Density = [100, 150, 120, 200, 220, 225, 190, 100, 300, 280, 301, 200, 110, 245, 450, 199, 200];
p_d = Population_Density;
figure(1)
plot(t, p_d, 'b-o', 'linewidth', 2)
title('Bacterial Population Density [2000,2016]')
xlabel('Time')
ylabel('Population Density cells/ml')
legend('Done Completely in MATLAB', 'location', 'northwest')
grid on;
filename = 'P_D.csv';
data = csvread('P_D.csv',1); % MATLAB won't start with data at the year 2000 and the population density at t = 2000; ?
TIME = data(:,2); % changing to 0 won't work, so I don't know what to do, the best I can do is start at 2001.
POP_DEN = data(:,4);
figure(2)
plot(TIME, POP_DEN, 'k-o', 'linewidth', 2)
title('Bacterial Population Density [2000,2016]')
xlabel('Time')
ylabel('Population Density cells/ml')
legend('Done with Excel sheet', 'location', 'northwest')
grid on;
3 commentaires
Réponse acceptée
Walter Roberson
le 17 Nov 2019
csvread() cannot handle text, except that you can tell it to skip leading rows and/or leading columns. So you can tell csvread() to skip the first row, which you are doing, but when you do so you are skipping the row that has the 2000 100 values.
You need to use xlsread() or readtable() or write your own reading routine, such as using textscan() or more low-level routines.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!