필터 지우기
필터 지우기

Creating a Matrix with Datenum column and a string column.

조회 수: 2 (최근 30일)
Davin
Davin 2014년 9월 17일
답변: Peter Perkins 2014년 9월 17일
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.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by