Using readxls alldata option

조회 수: 6 (최근 30일)
jnaumann
jnaumann 2015년 1월 2일
댓글: jnaumann 2015년 1월 2일
Hi,
I am using the readxls to import data into Matlab, the third output of which reads alldata - a subset of my data is as follows -
[332]
[320]
[319]
'ER4'
[744]
[319]
[319]
[772]
[320]
[763]
[734]
[320]
[763]
[744]
'74Y'
'77W'
[772]
'77W'
[346]
[380]
Some entries are double and some strings - I would like the data to be all in the same format (ideally a cell array of strings so I can use the unique function) however I am having some trouble. Does anyone know a way of achieving this?
Many thanks
Jack

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 1월 2일
You could try using cellfun to convert each element of your cell array (the alldata from above) to a string, though I suppose the performance of using this function would depend upon the size of your array. For example,
S = { [332]
[320]
[319]
'ER4'
[744]
[319]
[319]
[772]
[320]
[763]
[734]
[320]
[763]
[744]
'74Y'
'77W'
[772]
'77W'
[346]
[380] };
will create the cell array of numbers and strings. We could then apply the following which will convert each element from a number to a string
V = cellfun(@(x)num2str(x),S,'UniformOutput',false);
Note that for those elements of S that are already strings, then num2str has no effect on that element, and so returns the identical string value. In our case, V becomes
V =
'332'
'320'
'319'
'ER4'
'744'
'319'
'319'
'772'
'320'
'763'
'734'
'320'
'763'
'744'
'74Y'
'77W'
'772'
'77W'
'346'
'380'
and you should be able to apply the unique function to get
unique(V)
ans =
'319'
'320'
'332'
'346'
'380'
'734'
'744'
'74Y'
'763'
'772'
'77W'
'ER4'
Try the above and see what happens!
  댓글 수: 1
jnaumann
jnaumann 2015년 1월 2일
works perfectly thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by