我在MATLAB中使用xlsread从excel文件中读取表格.我的目标是将Excel工作表的每一列读取为数字数组.其中一列有数字和数字+字符组合.例如,值可以是200,300A,450,500A,200A,100.这是我到目前为止:
[num, txt, raw] = xlsread(fileIn, sheets{ii}); % Reading in each sheet from a for loop
myCol = raw(:, 4) % I want all rows of column 4
for kk=1:numel(myCol)
if iscellstr(myCol(kk))
myCol(kk) = (cellfun(@(x)strrep(x, 'A', ''), myCol(kk), 'UniformOutput', false));
end
end
myCol = cell2mat(myCol);
Run Code Online (Sandbox Code Playgroud)
这可以从数字中剥离出char,但随后我就离开了
myCol =
[200]
'300'
[450]
'500'
'200'
[100]
在cell2mat上有哪些错误:
cell2mat(myCol)
??? 在46处使用==> cell2mat时出错
输入单元阵列的所有内容必须具有相同的数据类型.
我觉得我可能在某处混淆了()和{}.有人可以帮我解决这个问题吗?
让我从阅读文件开始
[num, txt, raw] = xlsread('test.xlsx');
myCol = raw(:, 4);
idx = cellfun(@ischar,myCol ); %# find strings
data = zeros(size(myCol)); %# preallocate matrix for numeric data
data(~idx) = cell2mat(myCol(~idx)); %# convert numeric data
data(idx) = str2double(regexprep(myCol(idx),'\D','')); %# remove non-digits and convert to numeric
Run Code Online (Sandbox Code Playgroud)