2

I have a cell array containing historic gold price data and a cell array with the associated dates. I want to plot dates against prices for simple analysis but I am having difficulty converting the cell array of prices into a doubles.

My code is:

figure
plot(Date,USDAM,'b')
title('{\bf US Gold Daily Market Price, 2010 to 2015}')
datetick
axis tight

When I try to convert the gold prices (USDAM) into a double using cell2mat(USDAM), it throws the following error:

Error using cat Dimensions of matrices being concatenated are not consistent.

Error in cell2mat (line 83) m{n} = cat(1,c{:,n});

I use the following code to import the data:

filename = 'goldPriceData.csv';
delimiter = ',';
startRow = 2;
endRow = 759;
formatSpec = '%s%s%*s%*s%*s%*s%*s%[^\n\r]';

fileID = fopen(filename,'r');


dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', delimiter, 'EmptyValue' ,NaN,'HeaderLines', startRow-1, 'ReturnOnError', false);

fclose(fileID);

Date = dataArray{:, 1};
USDAM = dataArray{:, 2};
5
  • 2
    Post a minimal example with an input variable that reproduces the problem Commented Jan 2, 2015 at 13:03
  • are your dimensions correct? What of type are the elements in the cell? Commented Jan 2, 2015 at 14:19
  • Dates is of class cell of 758x1 which contains '2014-12-31' Commented Jan 2, 2015 at 14:33
  • USD is also a cell array of 758x1 which contains gold prices such as '1199.25' Commented Jan 2, 2015 at 14:34
  • when i use str2double(USDAM) it returns a 758x1 matrix of class double with values like '1.199250000000000e+03' as opposed to the correct price of 1199.25 Commented Jan 2, 2015 at 14:40

1 Answer 1

2

For your problem, cell2mat is the wrong function. Consider the cell array of strings:

>> S = {'1.2';'3.14159';'2.718'}
S = 
    '1.2'
    '3.14159'
    '2.718'
>> cell2mat(S)
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83)
            m{n} = cat(1,c{:,n}); 

That's because each row of the output needs to have the same number of columns, the strings are of different length. You can use strvcat(S) to get a rectangular matrix padded with spaces, but that isn't what you want. You want numeric data.

>> str2double(S)
ans =
    1.2000
    3.1416
    2.7180

Just because it puts a number in scientific notation, doesn't mean it's not the same number. That is, 1.199250000000000e+03 is 1199.25. To get the tick labels looking the way you want them, set YTickLabel property of the axis with formatted strings, the ones in USD.

Regarding your dates, you'll need numeric data to plot on each axis so convert the dates with datenum.

>> dateVals = datenum({'2014-12-30', '2014-12-31'})
dateVals =
      735963
      735964

Then to get the dates displayed on the x axis correctly, set the XTickLabel properties of the axis to get it looking how you want it (using the strings in Dates). Note that for setting the tick labels, you must ensure that you have the correct (same) number of ticks as labels that you indent to have. But that is a different question, I think.

Sign up to request clarification or add additional context in comments.

5 Comments

If I now want to forecast future price data using estimate regression how would I do this and show it on the same figure?
Mdl = regARIMA('ARLags',1,'MALags',1);
fit = estimate(Mdl,USDAM);
[Yf,YMSE,U] = forecast(fit,100,'Y0',USDAM);
Plot with hold on. Please ask a new question for more exact advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.