Weird .csv file plot problem
8 views (last 30 days)
Show older comments
Hey, When I plot from a csv file like so:
K=csvread(b);
plot(K);
I get the correct outcome but the x axis is not the first column as I would like it to be. So I tried this and it didn't work for some reason:
M=csvread(a);
x=M(:,1);
y=M(:,2);
l=M(:,3);
figure (1)
plot(x,y,'g');
hold on
plot(x,l,'r');
I do get a graph with the correct x axis, but the graph is missing many points unlike the previous way where it had all 700000 points. Do I have too many columns for the second way to work? I am using MATLAB 2013b. Thanks
2 Comments
Image Analyst
on 21 Jul 2018
You forgot to attach the files you're using for the badly-named a and b. Please attach them so we can try it and see what the difference is.
Accepted Answer
dpb
on 21 Jul 2018
Edited: dpb
on 21 Jul 2018
The first syntax per the documentation plots all columns of K versus ordinal number
You're on the right track with the second; you don't need the temporaries nor two calls to plot() however...
M=csvread(...;
plot(M(:,1), M(:,2:end))
will plot all columns from 2nd on vs the first; plot will use all points other than any which are NaN that are silently ignored; probably the difference in what you think is number of plotted points is that in the first case the x-axis scale will go to cover 1:N whereas in the second it will be scaled to the value of the elements in the first column which one presumes are not 1:length(M).
What does
sum(isfinite(M))
return at the command line??
Beyond that we'd have to see the data to be able to say anything more...
NB: There's little real point in plotting 700K points, even the most high resolution and largest monitor won't have more than a few K pixels so there's nowhere near enough to display all the points without serious aliasing. Decimating or decimating with bounding or the like would be a logical thing to do here...
ADDENDUM
The problem is that you didn't save enough precision in the input file to record the data accurately.
>> arrayfun(@(i) length(unique(m(:,i))),[1:3],'uniform',1)
ans =
5140 60 35
>>
Whether the 2nd/3rd columns are supposed to be more than such a few different values I don't know, but clearly the first column was supposed to be a time vector and it simply isn't precise enough for the size of the data.
Don't know who wrote the file, but there's where the basic problem comes from, not from plot()
5 Comments
dpb
on 21 Jul 2018
And I downloaded you file and plotted it and did the same test...
subplot(2,1,1)
hL1=plot(m);
x1=get(hL1,'XData');
cellfun(@length,x1)
ans =
700012
700012
700012
subplot(2,1,2)
hL2=plot(m(:,1),m(:,2:end));
x2=get(hL2,'XData');
cellfun(@length,x2)
ans =
700012
700012
>>
which shows it plotted all points either way...the appearance is a result of the fact you didn't save the data with sufficient precision for the first column--
u=unique(m(:,1));
numel(u)
ans =
5140
length(m(:,1))/numel(u)
ans =
136.1891
so there are on average 136 values plotted on top of each other with m(:,1) as the x vector.
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!