labeling x-axis with cell array - matlab

I have been searching various forums for hours but it seems impossible to do a thing in Matlab that's automatic in excel...
I used uiimport to import an xls file with into two arrays (? total newbie), one containing dates for my x-axis and the other the values I want to plot.
I have 180 values. The dates are three dates per month, more or less ranging from May 2008 until now, end of March.
Using
plot(mynumbers)
set(gca,'XTickLabel',dates)
only puts dates for May 2008 on my x-axis!
where did all the other dates go?
Instead using
plot(mynumbers)
set(gca,'XTick',mynumbers,'XTickLabel',dates)
gives error message
"??? Error using ==> set
Values must be monotonically increasing."
Please help!

where did all the other dates go?
The answer to your first question is that MATLAB only uses the first N number of strings corresponding to the default N number of tick marks on the x axis.
"??? Error using ==> set Values must be monotonically increasing."
The error is telling you that your date ticks must be evenly spaced. Instead of using dates corresponding to your actual data points, you could grab the x tick values that MATLAB automatically assigned to your graph, translate them to text, and then reassign the dates as x tick labels, like so:
% generate example unevenly spaced date vector
time = [now,now+1,now+25,now+28.5,now+36,now+40,now+51,now+65];
% generate random data points
data = rand(size(time));
% plot time vs data, storing the axes handle in the process
figure;
axH = axes;
plot(axH,time,data)
% get the x-axis tick locations
ticLoc = get(axH,'XTick');
% format tick labels (substitute any date format you wish)
ticLab = cellfun(#(x) datestr(x,'mm/dd'),num2cell(ticLoc),'UniformOutput',false);
% apply tick labels
set(axH,'XTickLabel',ticLab)
MATLAB's built-in function datetick also performs similarly.
However, if you zoom afterwards, you won't have accurate tick labels. So you may want to use datetick2 on the File Exchange.
If you're having trouble converting a cell array of dates from Excel into a numeric array, use:
dateNumeric = cell2mat(cellfun(#datenum,dateStrings,'UniformOutput',false));

try set (gca,'XTickLabel',num2str(dates))

Related

How to draw time based graphs using ios-charts

I'm trying to draw a temperature graph using iso-charts where the x axis data would be set from a server timestamp but the labels would be readable text.
For instance the graph x-axis label would start at Monday 00:00 and end Tuesday 12pm but the LineChartDataSet would be a collection of temperature (y-axis) and timestamps for the x
To display the timestamp I have a custom valueFormatter set as follow (which works great)
lineChartView.xAxis.valueFormatter = timestampXAxisFormatter() //converts timestamp to Date string
My question: The LineChartDataSet seems to be indexed based which is causing some trouble: if I have 4 data points such as (9am, 10), (9:15am, 11), (12pm, 15), (1pm, 16) the 4 points are set in the chart at regular intervals (I was expecting 2 points to be on the left side of the graph and then last 2 points on the right side) - Is there a way to have a data set that is based on the x value instead of the index?
I saw ChartData has an init that takes an array of NSObjects but then it converts it to Strings...Thanks in advance for any suggestions you may have!
There is no good way to solve it, as you figured out the x axis is index based.
You have two options:
insert many x values between each real x value, like between 9:00 and 9:15, you manually insert 9:01, 9:02, ..., 9:14, but don't add any entry at these values, just ignore it and continue. ios-charts will skip if no entry found and go to next. This will works fine, if you don't have a large number of values to insert. I tried ~1000 values, the performance is acceptable.
you create your own chart, using two y axis, one as x axis and one as y axis, so the distances to 0 point are calculated by value. However this requires you understand the ios-chart logic deeply. If you succeed, you are more than welcome to file a PR.

Unexpected date when ploting a timeseries MATLAB

I am trying to plot a timeseries using a cell array of strings representing an hour of measurements with a sample every 10 seconds. Below is the code I use to plot this data:
Voltages=[230.1,235.1,.......237];
Time={'13:00:10','13:00:20', '13:00:30'........'14:00:00'};
t=timeseries(Voltages, Time); % Using timeseries function in MATLAB
plot(t);
I also add two straight lines showing upper and lower voltage limits, and here is the chart I get:
As you can see in final result an unexpected date is shown on the x-axis ...
I want to do one of the following:
Remove the date altogether from the x-axis.
Using a date string 06.05.2015 I have in a variable, add this date instead of the 1-Jan-2015 unexpected date.
Remove the unexpected date from x-axis and then add the 06.05.2015 in text box.
From the documentation of timeseries class:
ts = timeseries(data,time) creates the time-series object using the specified data and time.
Where time above is a "time vector" as defined further down on the same documentation page:
Time Vector
A time vector of a timeseries object can be either numerical (double) values or valid MATLAB date strings.
When the timeseries TimeInfo.StartDate property is empty, the numerical time values are measured relative to 0 (or another numerical value) in specified units. In this case, the time vector is described as relative (that is, it contains time values that are not associated with a specific start date).
Before plotting the timeseries, try setting the TimeInfo.StartDate to what you need it to be, as shown in the example below:
Time = ['13:00:10';'13:00:20';'13:00:30'];
Data = [1 2.5 3];
start_date = '06.05.2015'; %// What you specified
ts = timeseries(Data,Time);
%// Since a date format separated with dots isn't supported in MATLAB, we replace . => /
ts.TimeInfo.StartDate = strrep(start_date,'.','/');
plot(ts)
Which results in:
                   

How do I plot on matlab with dates along the x-axis

I have two vectors(?) of data - one being prices, and the other the dates that those prices occured, and I am trying to plot a scatter plot of the two.
My dates are of the format ddmmyyyy and I have tried using mat2str to convert the vector into strings, and then used
formatin='ddmmyyy';
datenum(MYDATA,formatin)
however it returns an error saying that datenum has failed.
EDIT
This is the example of my code
This is what I am trying to run, where AvivaDate is a vector of 1200x1 double. The problem seems to be that mat2str is not changing the vector into a string of numbers: e.g. I need it in {'12345','12345'} form but mat2str is changing it into a string of '[12345' 12345]', so not a list of separate strings if that makes sense
formatin = 'ddmmyyyy';
DateAviva = mat2str(AvivaDate);
datenum(DateAviva,formatin);
hist(ReturnAviva,datenum(AvivaDate,formatin));
datetick('x','keepticks','keeplimits');
mat2str is not the function you want to convert between numbers and strings. mat2str has a very specific function (you see how it puts the [] around the output?). Use num2str, then convert it into a cell array:
S = num2str(AvivaDate); % should be 1200 x 8 char
C = mat2cell(S,ones(size(S,1),1)); % should be 1200 x 1 cell
dates = datenum(C,'ddmmyyyy'); % should be 1200 x 1 datenums
Although, depending where you're getting the information from in the first place, there may be a better way of reading the dates in from file, so you don't end up with a matrix of numbers where you want dates in the first place.

How do you use time as an axis in a Core Plot graph?

I have two arrays arr1 and arr2 containing datetime in dd/MM/yyyy HH:mm:ss and HH:mm:ss respectively.
For eg. arr1[0] contains 23/12/2011 09:15:30 while arr2[0] contains 09:15:30.
Note that arr1 will always contain today's date, which is exactly what I want for my iPad app which I'm preparing using Xcode 4.2.
I want to plot specific time (either from arr1 or from arr2 whichever is best suitable) on the X axis and corresponding float value contained in an array arr3 on Y-axis.
Now I'm stuck at plotting time on X axis as I can't pass time in dd/MM/yyyy HH:mm:ss or HH:mm:ss.
I googled about it and got the suggestion to use epoch but I'm unable to implement it for my app.
Other than using epoch, how can I pass a time value to Core Plot for the X axis?
Core Plot requires numeric plot data. You'll have to find a way to convert the dates to a number. One way is to convert each one to an epoch value as you mentioned. If the dates are not evenly spaced, that may be your only option.
If you know that the dates are spaced at regular intervals (e.g., hourly, daily, monthly, etc.) you could just figure an index that corresponds to each date. Make custom labels to display the dates and set the plot space range to cover the correct range of indices.

Core Plot skipped values Graph

i'm trying to draw a Graph with a user-friendly timeline having every day/week (to be decided by time range) as a label at x-axis. However, the datasource values are given on another basis - there might be 10 entries one day and the eleventh comes in a month.
See the photoshop image:
With the latest Core Plot drop I cannot find a way to do it, need your help.
Regards,
user792677.
The scatter plot asks the datasource for x-y pairs of data. There is no requirement that either value follow some sort of sequence or regular pattern. Other plot types work similarly, although the names and number of data fields can vary.
In your example, return 4 from the -numberOfRecordsForPlot: method. Return the following values from the -numberForPlot:field:recordIndex: method. The table assumes your y-values are calculated by a function f(x), but they can of course come from any source. Use the field parameter to determine whether the plot is asking for the x- or y- value.
Index X-Value Y-Value
0 1 f(1)
1 3 f(3)
2 9 f(9)
3 10 f(10)