Reputation: 5
i got data (2945 * 3) of different types imported as cell array. 1st column data type has been imported as text (time e.g 1/1/1990), whereas the 2nd and 3rd columns are numbers.
so far i used cell2mat to convert to double both the 2nd and 3rd columns. Thus plot(y) works {y being either the 2nd or 3rd column data} , however i am wondering how i can handle the text data type from my 1st column in an attempt to use plot(x,y).
Any idea would be appreciated. cheers
--------sample.csv-------------
Date LAST Rt
1/27/2018 20 0.234556
1/26/2019 20.05 0.184556
1/23/2040 20.1 0.134556
1/22/1990 20.15 0.084556
1/21/1991 20.2 0.034556
1/20/1993 20.25 -0.015444
1/19/1998 20.3 -0.065444
1/16/2050 20.35 -0.115444
1/15/2030 20.4 -0.165444
--------cell array appearance------------
1 | 2 | 3
1| '1/27/2018' 20 0.234556
2| '1/26/2019' 20.05 0.184556
3| '1/23/2040' 20.1 0.134556
4| '1/22/1990' 20.15 0.084556
5| '1/21/1991' 20.2 0.034556
6| '1/20/1993' 20.25 -0.015444
7| '1/19/1998' 20.3 -0.065444
8| '1/16/2050' 20.35 -0.115444
9| '1/15/2030' 20.4 -0.165444
Upvotes: 0
Views: 259
Reputation: 13876
You could also use datenum
to convert the text to a serial date number (copied from Octave command line):
>> test
test =
{
[1,1] = 1/1/2000
[1,2] = 1/2/2001
[1,3] = 10/2/2001
[1,4] = 10/3/2001
[1,5] = 10/3/2005
}
>> x_dates = cellfun('datenum',test(:,1))
x_dates =
730486 730853 731126 731127 732588
>> y = rand(size(x_dates));
>> plot(x_dates,y)
>> datetick('x','dd/mm/yyyy')
Update:
It looks like cellfun
requires a function handle in MATLAB, so you probably need to do something like:
x_dates = cellfun(@datenum,test(:,1))
Upvotes: 1
Reputation: 106
You can create a new matrix with usable data (or rewrite your current one) by looping through your matrix(:,1) to convert the strings using the cellfun function.
Or just to plot:
plot(cellfun(@(x)str2double(x), Measures(:,i)))
where i = 1: length(matrix)
Upvotes: 0
Reputation: 35525
You could use XTick
and XTickLabel
. The former will set up where and how many ticks you want in your X axis (I guess that you'd want one for each X data, but you also may want to go jumping 10 by 10). The second will set the labels in those tick positions. If the Labels are less than the ticks, they will repeat, so careful with that.
Let me illustrate with an example:
x = [0 1 2 3];
y = [2 0 1 1];
plot (x, y);
yourstrings={'Banana', 'T', 'Potato', '45'};
set(gca,'XTick',x(1):x(end))
set(gca,'XTickLabel',yourstrings)
A second option would be to use text
. you could put text wherever you like in the plot. Let me illustrate again. Of course I don't meant to put it "nice", but if you/d like, you could play with offsetting the positions of the texts and so on in order to get a more "beautiful" plot.
x = [0 1 2 3];
y = [2 0 1 1];
plot (x, y);
yourstrings={'Banana', 'T', 'Potato', '45'};
for ii=1:length(yourstrings)
text(x(ii),y(ii),yourstrings{ii})
end
Upvotes: 1