李旭分享 http://blog.sciencenet.cn/u/lixujeremy 代码与散打爱好者!

博文

Matlab Read Excel Data with String and Numeral

已有 2902 次阅读 2013-11-21 21:24 |个人分类:Matlab|系统分类:科研笔记| MATLAB, EXCEL

Sometimes, Data was saved as Excel file format. And, it often contains strings and numerals in a column, we all know that Matlab reads the data as a m-by-n Cell Arrary. It is difficult to get and set all together, the following is an example to convert numerals to string in Cell Arrary.


Main Program:

clear;

clc;

% Read Two.xlsx For Values

% Num is an Arrary in Double, all numeralsare in it, and non-numerals all are filled in NaN.

[Num, ~, Values2]=xlsread('Two.xlsx','A1:BXQ1356');  

% Convert Values to CellStr Every Column

for ii=1:size(Num, 2)

  Values2(:, ii)=ReAssign(Num(:, ii), Values2(:, ii));

  fprintf('%d Col done!n', ii);

end

clear Num

 

Function ReAssign:

% Created by LI Xu

% 21 November, 2013

% Version 1.0

% Description

% Data from Excel Tables can be assigned toanother file

% in order.

 

function ColCells=ReAssign(ColNum, ColRaw)

num=ColNum;

raw=ColRaw;

 

% Index of Txt

Num=num2str(num);

Num=cellstr(Num);

Num=strfind(Num, 'NaN');

% Whether Empty Set

Index_Txt=logical(size(Num));

for ii=1:length(Num)

  Index_Txt(ii)=~isempty(Num{ii});

end

Index_Txt=Index_Txt';

 

% Index of Number

Index_Num=~Index_Txt;

raw(Index_Num)=cellstr(num2str(num(Index_Num)));

 

ColCells=raw;






https://blog.sciencenet.cn/blog-1148346-743627.html


下一篇:Introductions for ENVI Compiled Procedures?
收藏 IP: 114.255.218.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-7 23:33

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部