gpt4 book ai didi

matlab - 将非均匀元胞数组转换为数值数组

转载 作者:太空宇宙 更新时间:2023-11-03 19:32:30 24 4
gpt4 key购买 nike

我在 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);

这能够从数字中去掉字符,但我剩下的是:

myCol = 

[200]

'300'

[450]

'500'

'200'

[100]

cell2mat 上出现的错误是:

cell2mat(myCol)

??? Error using ==> cell2mat at 46

All contents of the input cell array must be of the same data type.

我觉得我可能在某处混淆了 (){}。有人可以帮我解决这个问题吗?

最佳答案

让我从阅读文件开始

[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

关于matlab - 将非均匀元胞数组转换为数值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8608594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com