changing the x axis candle stick chart

drawing this candle stick chart gives me the following error
candle(col2,col3,col4,col1,'b',tt,'dd-MM-yyyy HH:mm:SS')
Error using candle (line 132) Function 'subsindex' is not defined for values of class 'cell'.
I just want the the x-axis of the candle stick chart to show the dates which i have saved in a datetime array.

 Accepted Answer

Nick Counts
Nick Counts on 6 Nov 2016
Edited: Nick Counts on 6 Nov 2016
Sorry, it looks like I misunderstood your issue. Without seeing what your variables are (data types, sizes, etc) it's a little tricky to debug your code, but here is an example with two approaches that should help:
% Load Sample Data
load disney;
% Build a vector of datenums for the x axis
dateNumVector = floor(now)-20:floor(now)-10;
dateNumVector = dateNumVector'; % Must be a column vector
% Method 1: Using a datenum array:
fh1 = figure;
% candle plot with the x-axis datenum argument
candle( dis_HIGH(end-20:end-10), ...
dis_LOW(end-20:end-10), ...
dis_CLOSE(end-20:end-10),...
dis_OPEN(end-20:end-10), ...
'b', ...
dateNumVector);
% Method 2: Using datetime object array
fh2 = figure;
% build an example datetime object array:
dateTimeArray = datetime(datestr(dateNumVector));
% candle plot with x-ais date argument
candle( dis_HIGH(end-20:end-10), ...
dis_LOW(end-20:end-10), ...
dis_CLOSE(end-20:end-10),...
dis_OPEN(end-20:end-10), ...
'b', ...
dateTimeArray.Day);
Note that in your sample line of code, if class(tt) is datetime then you have to use the Day method to pass dates in the form candle is expecting.
Hopefully this will get you sorted out :)

5 Comments

thanks but it still doesnt work. my datetimearray file is in the following format:
'18-Sep-2014 16:33:00'
'18-Sep-2014 17:26:00'
'18-Sep-2014 18:22:00'
'18-Sep-2014 19:14:00'
'18-Sep-2014 20:11:00'
'18-Sep-2014 21:04:00'
'18-Sep-2014 21:56:00'
'18-Sep-2014 22:48:00'
'18-Sep-2014 23:40:00'
'19-Sep-2014 00:33:00'
'19-Sep-2014 01:25:00'
'19-Sep-2014 02:18:00'
'19-Sep-2014 03:10:00'
'19-Sep-2014 04:02:00'
candle(col2,col3,col4,col1,'b',tt.Day)
Error using subsindex Function 'subsindex' is not defined for values of class 'cell'.
Error in candle (line 132) set(hcdl_vl, 'XData', dateset(line_xdata));
Without seeing your code, this is tough. If you could post the code, or a small example that would really help us understand what's going on.
I don't think I understand the data type of your variable tt.
If it is a datetime then it is an object with specific methods.
From your last comment, it sounds like maybe you have a cell array of strings.
If that is the case, then you want to look at
doc datenum
datenum will take a cell array of strings as an input and convert it to a double that uses Matlab's convention for representing time.
% Method 3: Convert a cell array of strings to dates
fh3 = figure;
% build an example cell array of strings:
cellArrayOfStrings = {
'18-Sep-2014 16:33:00';
'18-Sep-2014 17:26:00';
'18-Sep-2014 18:22:00';
'18-Sep-2014 19:14:00';
'18-Sep-2014 20:11:00';
'18-Sep-2014 21:04:00';
'18-Sep-2014 21:56:00';
'18-Sep-2014 22:48:00';
'18-Sep-2014 23:40:00';
'19-Sep-2014 00:33:00';
'19-Sep-2014 01:25:00';
'19-Sep-2014 02:18:00';
'19-Sep-2014 03:10:00';
'19-Sep-2014 04:02:00'};
% build a set of example stock data (doubles)
col1 = randi(100, 14, 1);
col2 = randi(100, 14, 1);
col3 = randi(100, 14, 1);
col4 = randi(100, 14, 1);
% candle plot with x-ais date argument from cell array of strings
candle( col1, ...
col2, ...
col3, ...
col4, ...
'b', ...
datenum(cellArrayOfStrings));
Hopefully that helps. If it doesn't, please try to post your code and we'll get it sorted.
hhh=fix(NewMATRIX2(:,2)/60);
mmm=mod(NewMATRIX2(:,2),60);
outtt=arrayfun(@(x,y) [sprintf('%d',x) ':' sprintf('%d',y)],hhh,mmm,'un',0);
kkk=datenum(outtt,'HH:MM');
outtt=datestr(kkk,'HH:MM');
t = datetime(outtt,'InputFormat','HH:mm');
tt = datetime(NewMATRIX2(:,1),'ConvertFrom','datenum');
t=timeofday(t);
tt=tt+t;
tt=tt';
plot(tt,col4,tt,col1);
numOnly=numOnly(:,1);
BB=numOnly(~any(isnan(numOnly),2),:);
graphdate=datetime(date_num(:,1),'ConvertFrom','datenum')...
+days(BB);
NewMATRIX2 is a matrix with time in minutes NewMATRIX2 is a matrix with date serial number.
the final output is the above datetime array which I cannot display in the candle chart.
AA, I think it would be really helpful if we could see a sample of NewMATRIX2. Do you mean that the first column is minutes and the second column is the serial date (as in, the output of Matlab's datenum)?
And forgive me if this sounds condescending, as it is not my intention, but do you intend to use duration objects and datetime objects?
If you don't need the class methods, it may be much simpler (and clearer) to use good-old serial dates, which are just represented as doubles and are supported by virtually all of Matlab's plotting functions (including candle)
Unfortunately, I can't run your sample code without guessing at what NewMATRIX2, date_num, and numOnly are.
This is my new guess:
Based on your previous question regarding importing excel times, I suspect this all stems from trying to get a usable representation of a timestamp (I will use timestamp to mean date and time) that you can use as the x-axis variable in your candle plot and also to specify the labeling of the x-axis on your plot.
Please post an example of your input (whatever variables you are starting with). It would also be very helpful to include sample code that we can copy-paste and run. You can include a sample variable like:
NewMATRIX2 = [736613, 60;
736614, 78;
736615, 42;
736616, 124;
736617, 256;
736618, 38]
We can get figure this out, we just need to know a little more :)
This is an educated guess based on your other questions:
clear
inputArray = {
'Mon, Apr-25-16' 0.0763888888888889;
NaN NaN;
'Mon, Apr-25-16' 0.0763888888888889;
'Fri, Mar-25-16' 0.0347222222222222;
'Wed, Feb-24-16' 0.0347222222222222;
'Tue, Jan-26-16' 0.0347222222222222;
'Fri, Dec-25-15' 0.0347222222222222;
'Wed, Nov-25-15' 0.0347222222222222;
};
% Get rid of nan
rowsToRemove = isnan([inputArray{:,2}]');
inputArray(rowsToRemove,:) = [];
dateMatrix = datevec(inputArray(:,1),'ddd, mmm-dd-yy');
% make the Matlab serial date
timeStamp = datenum(dateMatrix);
% Add the 'time' portion, which is a fraction of a day
timeStamp = timeStamp + [inputArray{:,2}]' ;
% build a set of example stock data (doubles)
col1 = randi(100, 7, 1);
col2 = randi(100, 7, 1);
col3 = randi(100, 7, 1);
col4 = randi(100, 7, 1);
% candle plot with x-ais date argument from cell array of strings
candle( col1, ...
col2, ...
col3, ...
col4, ...
'b', ...
timeStamp, ...
'dd/mm/yy');

Sign in to comment.

More Answers (2)

Nick Counts
Nick Counts on 4 Nov 2016
Edited: Nick Counts on 4 Nov 2016
try dateaxis
load disney;
candle(dis_HIGH(end-20:end), ...
dis_LOW(end-20:end), ...
dis_CLOSE(end-20:end),...
dis_OPEN(end-20:end), 'b');
dateaxis;
There are options for how the dates are displayed, see the documentation
Good luck!
AA
AA on 5 Nov 2016
no, datetick wont allow me to insert my prespecified x-axis (variable tt). is there any other way to integrate this?

Categories

Asked:

AA
on 4 Nov 2016

Commented:

on 9 Nov 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!