Info

此问题已关闭。 请重新打开它进行编辑或回答。

Date conversion program error

1 次查看(过去 30 天)
Michael
Michael 2011-7-5
关闭: MATLAB Answer Bot 2021-8-20
I have a program that finds the time column in a data sheet (in unix time) and takes each time value and makes a new excel document and converts it into the hour.
Here's the code I made for it: fileid=['sitedata.csv']; [data, text] = xlsread(fileid); col = strcmpi(text(1,:),'Time'); A = data(:,col); for i = 1:length(A) X = datenum([1970 1 1 0 0 A(i)]); hour = datestr(X, 'HH'); A(i) = hour; end
When I run it the error I get is: ??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> time at 28
A(i) = hour;
Any ideas how to fix this? Thank you!

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-7-5
In your code, "A" is a double array, "hour" is a char array. If you change A(i)=hour to B{i}=hour, it will work. But it's better to pre-allocate B as a cell array right before the for-loop B=cell(size(A));
  2 个评论
Michael
Michael 2011-7-5
With the first method, it's now giving this error:
??? Cell contents assignment to a
non-cell array object.
Error in ==> time at 28
A{i} = hour;
Fangjun Jiang
Fangjun Jiang 2011-7-5
No. You can't re-use the variable A. Try to make a different variable B.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by