Converting letters to strings

조회 수: 2 (최근 30일)
antonet
antonet 2014년 2월 23일
답변: Image Analyst 2014년 2월 25일
Dear Matlab users
I read some data from an excel file. The column 11 of this file contains
Baa1
Ca
Baa
B
Aaa
and so forth. When I use
raw=xlsread('file.xlsx');
K=raw(:,11)
I get NaN for each element of K. My questions is how to convert column 11 to a column of string variables. Note that my column is of dimension 20000 by 1. Is there a command in Matlab to do that?
Thanks in advance

채택된 답변

per isakson
per isakson 2014년 2월 23일
편집: per isakson 2014년 2월 23일
The documentations says:
[ num, txt, raw ] = xlsread( filespec );
Thus try
[~,~,raw] = xlsread('file.xlsx');
Now, K will contain the column of strings. You could also use the argument, txt, but then the column number might be "shifted" depending on whether there are numerical columns to the left (C<=10).
"column of string variables" do you mean a character array or a cell array of strings?

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 2월 25일
If you have R2013b you should use readtable() instead of xlsread(). It's so so much easier. Instead of all that nonsense of 3 cell arrays, which aren't even matched up as far as rows and columns go, you get a simple and intuitive "table" - which is a new variable class in R2013b that I encourage everyone to try.
ONCE YOU TRY readtable(), YOU'LL PROBABLY NEVER USE xlsread() EVER AGAIN.
Another use of the table is to use with regionprops() and struct2table() to create a table instead of an array of structures. I'll contact the Image Processing team to see if they can make a table an option directly instead of having to use struct2table.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by