我有一个 .xls 文件,我想通过 xlsread 函数将它导入到 Matlab 中。我得到带有工程符号的数字的 NaN ......就像我得到 15.252 B 或 1.25 M 的 NaN 有什么建议吗?
更新:我可以使用[num,txt,raw] = xlsread('...')
原始的正是我想要的,但是如何用 (*10 6 ) 替换 Ms?
编辑:
Matlab 不提供任何内置的工程格式字符串格式。
资料来源:http ://se.mathworks.com/matlabcentral/answers/892-engineering-notation-printed-into-files
在源代码中,您还会发现对您有帮助的功能。
首先,您可以使用 excel 从单元格数组中提取所有内容
[~,~,raw] = xlsread('MyExcelFilename.xlsx')
然后,您可以编写一个简单的函数,该函数根据“B”、“M”等从字符串中返回一个数字。这是一个这样的例子:
function mynumber = myfunc( mystring )
% get the numeric part
my_cell = regexp(mystring,'[0-9.]+','match');
mynumber = str2double(my_cell{1});
% get ending characters
my_cell = regexp(mystring,'[A-z]+','match');
mychars = my_cell{1};
% multiply the number based on char
switch mychars
case 'B'
mynumber = mynumber*1e9;
case 'M'
mynumber = mynumber*1e6;
otherwise
end
end
当然还有其他方法可以将数字字符串与其他字符串分开,使用你想要的。有关更多信息,请参阅regexp
文档。最后用于cellfun
将元胞数组转换为数值数组:
my_array = cellfun(@myfunc,raw);