1

我有一个 .csv 文件,其中包含一些数据,例如 date(30/10/2013)、closePrice(361.08)、volume(4500014)、openPrice(362.62)、highPrice(365)、lowPrice(358.65)。该文件包含 2510X6 的数据,我想绘制一个烛台图,有人可以帮我吗?这就是我所做的:

fid = fopen('Amazon.csv');
HDRS = textscan(fid,'%s %s %s %s %s %s',1, 'delimiter',',');
DATA = textscan(fid,'%s %f %f %f %f %f','delimiter',',');
fclose(fid);
outCell = cell(size(DATA{1},1), length(HDRS));

for i = 1:length(HDRS);
    if isnumeric(DATA{i});
        outCell(:,i) = num2cell(DATA{i});
    else
        outCell(:,i) = DATA{i};
    end
end

 candle (outCell{:,5}, outCell{:,6}, outCell{:,2}, outCell{:,4}, 'b', outCell{:,1}); 

运行文件时,我收到一条错误消息Error using candle Too many input arguments。我正在使用数组单元格,因为我有日期并将日期转换为向量我决定使用数组单元格。

4

2 回答 2

1

Curly-bracket derefencing, as in outCell{:, 5} in your call to candle, expands to what Matlab calls a "comma-separated list". Whenever you see curly-bracket dereferencing, you can think of it as being exactly equivalent to typing out the separate elements that are implied, separated by commas---so if size(outCell, 1) is 3, then this is as if you had typed outCell{1, 5}, outCell{2, 5}, outCell{3, 5}. That's three input arguments to candle right there, where you thought you were passing just one.

I'm unfamiliar with candle itself, but if it wants a single-column cell array as its first argument, then the way to get a single-column cell array out of outCell is to slice it with ordinary round-bracket dereferencing: outCell(:, 5)

If on the other hand candle wants a numeric vector rather than a cell array, you can say cell2mat(outCell(:, 5)). Another way (and this second example is where the power of curly-bracket dereferencing and comma-separated lists becomes apparent) would be to say [outCell{:, 5}]' - that's a comma-separated list, caught inside square brackets, which means horizontal concatenation of the elements.

于 2013-11-21T23:27:45.603 回答
0

我找到了以下方法来做到这一点:

首先,我观察到您需要列向量格式的日期,而不是cell. 实现这一点的唯一方法是将日期转换为某种数字表示。这正是这样datenum做的。示例如下:

DateString = '11/12/2013';
formatIn = 'mm/dd/yyyy';
datenum(DateString,formatIn)

ans = 

  735550

以这种格式转换所有日期。接下来,我觉得如果你构造时间序列对象,那么像这里所示的那样绘制会容易得多。这需要一个金融时间序列对象才能工作。没问题。它可以按此处所示的方式构建。在这种情况下,我相信它可以构造为(虚拟示例):

 dates={'11/12/2013';'11/13/2013'}
 higPrice=[100;100]
 lowPrice=[10;10]
 closePrice=[90;80]
 openPrice=[80;70]

 %construct a financial time series object
 tsobj = fints(datenum(dates,formatIn), [higPrice lowPrice closePrice openPrice], {'high','low','close','open'})  %put in correct order

 candle(tsobj);  %I get the plot

编辑:我忘了提到,如果我尝试给出任何其他名称, 'high','low','open','close'则它不起作用。例如,我尝试使用'highPrice','lowPrice','openPrice','closePrice'. 我不知道这是什么原因,因为我也是candle第一次使用。

于 2013-11-21T23:35:34.887 回答