0

I have a csv file that has two columns, date and float, the date format is weird though (Jan 1 2016 9:55:00 PM), how can I import it to matlab and draw it as a plot? this is what i tried:

fid = fopen('all.csv');
if fid>0    
     data = textscan(fid,'%s %d','Delimiter',',');
     % close the file
     fclose(fid);         
end
x = data(:,1);
y = data(:,2);

plot(x,y);

but i get an error not enough arguments

Sample of all.csv:

Jan 1 2016 9:55:00 PM, 12493829,
Jan 2 2016 7:55:00 AM, 83747283,
Jan 3 2016 2:55:00 PM, 89572948,
Jan 4 2016 8:55:00 AM, 95862744,

the error:

Error using plot
Not enough input arguments.

Error in test (line 10)
plot(x,y);
Abdane
  • 137
  • 1
  • 12

2 Answers2

2

The error appears since you're inputting cell arrays in the plot function.

x = data{:,1};   %You need { } here, not ()
y = data{:,2};

plot(1:numel(x), y, 'o-');
%Ensuring the xticks are as required and changing the xticklabels to datetime values and 
%rotating the labels for better visualisation ('xticklabelrotation' requires >= R2014b)
set(gca,'xtick',1:numel(x),'xticklabel',x,'xticklabelrotation',45);  

Output:

output

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • Works perfect. I tested it with the sample i posted, but the data I have has three million rows, I am using the online compiler and it is not showing any figures, maybe because it is huge, once I get home i will test it on my matlab on laptop, do you think it will work for 3 million rows? – Abdane Nov 24 '17 at 19:11
  • @Abdane It will but I don't know how you'll be able to obtain any useful information from the plot made with that many rows – Sardar Usama Nov 24 '17 at 19:13
  • that's fine, ill test it when i get home, quick question, how can I remove the time from my string? I just need to show the date without the time. is there any substring equivalent in Matlab> im new, sorry – Abdane Nov 24 '17 at 19:16
  • found it, extractBefore(str, int); thanks anyways :) – Abdane Nov 24 '17 at 19:20
  • unfortunately it didnt work with 3mil rows, is there any other approach you think i can use? – Abdane Nov 24 '17 at 21:25
  • I ran it, it ends and nothing happens, no plot is shown – Abdane Nov 24 '17 at 21:27
  • do you get anything if you don't use the last line? – Sardar Usama Nov 24 '17 at 21:29
  • Yea i do, but damn the graph is ugly, haha, looks like a barcode :P It was very different with excel – Abdane Nov 24 '17 at 21:32
  • 1
    seems like you need better graphics. and very likely not a programming issue. plot a small version of data that you need to visualise at one moment instead. – Sardar Usama Nov 24 '17 at 21:40
0
fid = fopen('all.csv');
if fid>0    
     data = textscan(fid,'%s %d','Delimiter',',');
     % close the file
     fclose(fid);         
end
x = datenum( data(:,1),'mmm dd yyyy HH:MM:SS PM');Convert it to a compatoble format before
y = data(:,2);
plot(x,y);
datetick('x','dd.mm.yyyy','keepticks') %Makes the x axis numbers to dates
Squeezie
  • 361
  • 2
  • 14