Creating a Matrix with Datenum column and a string column.

2 次查看(过去 30 天)
Hello,
I would like to know how to deal with this issue, I cant seem to find a proper answer.
I have the following matrix :
[735859] 'Joint'
[735901] 'Single'
[735950] 'Joint'
[735992] 'Single'
[736041] 'Joint'
[736083] 'Single'
[736132] 'Joint'
[736174] 'Single'
[736224] 'Joint'
[736265] 'Single'
[736314] 'Joint'
The first column is a datenum and I want to create a matrix without the brackets for the first column and the second column as it is.
So I did the following :
>> CL = [cell2mat(Cal1(:,1)), cellstr(Cal1(:,2))]
I would expect it would remove the brackets and keep the string column.
(Removing the brackets will permit me to use datestr...)
But the answer I get is
Error using horzcat Dimensions of matrices being concatenated are not consistent.
The dimensions are identical for me...
Can someone enlightened me please?
Thank
Davin
  2 个评论
dpb
dpb 2014-9-17
How about telling us how you got the array as it is and perhaps the solution is to not generate it in that format in the first place. I'm thinking if it's being read in then the 'collectoutput' parameter in textscan might just be the trick.
Davin
Davin 2014-9-17
Guillaume did help me with his answer but nevertheless to answer your question, I got this array from Spreadsheet Link EX. I sent some array from XL to Matlab. May be the way it exports the data is done in some format, but right now I have no control on the export settings... Thank you dpb.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2014-9-17
编辑:Guillaume 2014-9-17
First, a point of semantic, what you have is a cell array, not a matrix. Two completely different beasts.
It's not particularly clear what you want as the brackets (and the quotes for that matter) are just the way matlab display the content of the cell array. It's not part of the content.
If you want to convert the content of the 1st column from datenum to datestr, it's simply:
c = {[735859] 'Joint'
[735901] 'Single'
[735950] 'Joint'
[735992] 'Single'
[736041] 'Joint'
[736083] 'Single'
[736132] 'Joint'
[736174] 'Single'
[736224] 'Joint'
[736265] 'Single'
[736314] 'Joint' };
datestr([c{:, 1}])
If you want to replace the datenum by datestr:
c(:, 1) = cellstr(datestr({c{:, 1}]));

更多回答(1 个)

Peter Perkins
Peter Perkins 2014-9-17
Davin, as others have said, you have a cell array. That's one way you can store data of different types in one array, but it may not be the most convenient way.
An alternative, if you are using MATLAB R2013b or later, would be to use a table, and maybe also a categorical array. For example
>> c = {[735859] 'Joint'
[735901] 'Single'
[735950] 'Joint'
[735992] 'Single'
[736041] 'Joint'
[736083] 'Single'
[736132] 'Joint'
[736174] 'Single'
[736224] 'Joint'
[736265] 'Single'
[736314] 'Joint' };
>> t = cell2table(c,'VariableNames',{'Date' 'Type'});
>> t.Type = categorical(t.Type)
t =
Date Type
__________ ______
7.3586e+05 Joint
7.359e+05 Single
7.3595e+05 Joint
7.3599e+05 Single
7.3604e+05 Joint
7.3608e+05 Single
7.3613e+05 Joint
7.3617e+05 Single
7.3622e+05 Joint
7.3627e+05 Single
7.3631e+05 Joint
>> t(t.Date>datenum('1-Feb-2015') & t.Type=='Joint',:)
ans =
Date Type
__________ _____
7.3604e+05 Joint
7.3613e+05 Joint
7.3622e+05 Joint
7.3631e+05 Joint
Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by