How to specify the format of just one column with readtable?

조회 수: 42 (최근 30일)
I M
I M 2016년 6월 21일
댓글: I M 2016년 6월 23일
Hi,
I am using readtable to read a .xls file with header information. All the columns in the table are formatted correctly, except one where I have time of day being written in hh:mm format.
Since I only need this information for further reference and no subsequent computations, I was trying to assign the format of this specific column to string by writting readtable(myxls,'Format','%s'). However, I am unable to assign the format to just this column (in this specific case, the 2nd column). Is there any way I can assign the format of this column, while leaving the other untouched, and without having to specify the format for every single table column?
Thanks in advance for all the advice you can provide.
  댓글 수: 2
Shameer Parmar
Shameer Parmar 2016년 6월 22일
can you please specify the input data along with its format and expected data with format..
For excel read, please use command xlsread()
I M
I M 2016년 6월 23일
Sorry, my bad. The input data is a table with 12 columns. Each column has either numeric data or string formatted data. The only weird column is a column with time, where time is written in hh:mm format (for example 10:15).
All numeric and string columns are being imported in the correct format into a table in matlab using readtable, except this time column, which is being converted to a float point number (e.g. 0.4325).
I would like to format this column to string, but I cannot define it just for this column with readtable using 'Format', unless I define the format for every column on the table. Is there a simple way of assigning the format of just this column with readtable?
I have also tried xlsread() but most columns come in the wrong formatting.

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

채택된 답변

Guillaume
Guillaume 2016년 6월 23일
편집: Guillaume 2016년 6월 23일
Please ignore Shameer's comment about using xlsread instead of readtable. They provide exactly the same support of xls file ( readtable calls xlsread internally), so if one does not work, the other won't work any better. If anythin readtable is better since it provides extra functions on top of xlsread.
Unfortunately, at the moment*, you cannot specify formats for columns of excel files. Note that the Format option only works for text files, not excel files.
In any case, matlab has read your time correctly, but as datenum. You can convert it to date strings with:
%t: your table
%timecolumn: the column that is read as datenum
t.timecolumn = datestr(t.timecolumn, 'HH:MM')
*Note: you will be able to specify individual formats in the next version of matlab.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by