【问题标题】:How do I convert strings stored in a cell array to numbers in Matlab?如何将存储在元胞数组中的字符串转换为 Matlab 中的数字?
【发布时间】:2016-09-30 18:20:38
【问题描述】:

我有一个包含 4 或 5 个字符的字符串的 excel 列,我想将它们转换为数字。因此,我希望单元格包含“5”而不是“关闭”。而不是“打开”,我希望单元格包含“4”

[num,txt,raw] = xlsread('test.xlsx');

instructionopen = find(strcmp(raw,'open'),2);
instructionclose = find(strcmp(raw,'close'),2);

% I will try to convert a string cell to characters and the to a number. 
open = [] 

open = [] 
open2 = []
for i = 1:numel(instructionopen)
    open = [open; char(instructionopen(i))];  
    open2 = [open2; str2num(open(i))];
end

我确信我的编码并不优雅,但我基本上都在尝试创建两个数组,一个将每个字符串元素转换为一个字符,然后另一个将它们转换为数字。转换为字符后的输出虽然给了我一个看起来像 2 个汉字的值(!)。那一步我一定是在做一些事情。

我做错了什么,或者我怎样才能在这些单元格中获得数字而不是字符串?

编辑

beaker 建议使用地图容器。我试过了

instructions   = {'open','close'};
valueSet = [1,2];
mapObj = []

for i = 1:numel(instructionopen)
    if instructionopen(i) == 'open'
        mapObj = containers.Map(instructions,valueSet) 
    end
end

但问题是 mapObj 出来是空的,所以我一定是做错了。

matlabbit 建议使用字符串的长度。我试过了

instructiontype = []
for h = 1:length(instructionindex)
    instructiontype = [instructiontype; length(instructionindex(h))]
end

但它一直给我数组中元素的长度为 1。我找不到函数 strlength?

EDIT2 matlabbit 建议使用 cellfun(length)

instructionindex = find(strcmp(raw,'Instruction: ')); %gets index of cells that have "Instruction" in them
instructionindex2 = char(raw{instructionindex, 2}); %converts the content of the cells one column to the right (second column) to characters
instructionindex3 = mat2cell(instructionindex2, ones(size(instructionindex2,1),1)); %converts this to a matrix of cells
instructionindex4 = cellfun('length',instructionindex2) %finally performing the conversion to the number that represents the length of the content of each cell

我现在得到了 24 个双打的矩阵,但它们都是五 (5)...但有些应该是四 (4)。

如果我没有完成 mat2cell,它就不允许我执行长度函数,因为我不断收到它们不是单元格的错误。可能是因为在那之前我使用了 char,我得到了一个看起来像 closecloseclosecloseopenopenopencloseopenopenopenopen...ect 的值

编辑 3:张贴图片作为简化 excel 文件的示例

我正在设法提取条件和试验的数字,但我需要为“打开”和“关闭”提供一些数字。我正在尝试将它们转换为相应的字符串长度。

我也试过这个:

instructionindex = find(strcmp(raw(:,1),'Instruction: '));
instructionindex1 = char(raw{instructionindex,2});
instructionindex2 = mat2cell(instructionindex1, ones(size(instructionindex1,1),1));
instructionindex3 = cellfun('length',instructionindex2);


test = zeros(size(instructionindex2));
for h = 1:length(instructionindex2)
    if strcmp(instructionindex2{h},'open')
        test(h) = 4;
    elseif strcmp(instructionindex2{h},'close')
        test(h) = 5;
    end
end

有趣的是,这给了我数字 (!),但数组包含 0 表示打开,5 表示关闭。我想我现在对此很好,但我真的想知道我做错了什么!

【问题讨论】:

  • 也许您正在寻找一个 Map 容器,它可以将字符串作为键映射到数值。
  • 您想要数字 5 还是文本表示“5”?此外,是否与具有 5 个字符的“关闭”相关?这真的只是一个 strlength 操作吗?
  • 感谢@beaker 的建议。我用我认为的解决方案编辑了我的问题,但我仍然一定做错了..
  • 谢谢@matlabbit!我需要一个数字,因为当我的单元格不是数字时尝试连接时,我害怕遇到的所有限制。我在使用这些字符串时遇到的问题确实让一切变得如此复杂。我按照你的建议尝试了一些东西,并编辑了我的问题,但它没有用..
  • 我怀疑你一直得到 1 因为你没有取消引用单元格。指令索引(h)将返回一个标量单元。 instructionindex{h} 将返回 h 处单元格的内容。

标签: arrays string matlab


【解决方案1】:

给定

raw = {'open','close','open','close'};

要得到长度,你可以从 16b 开始:

strlength(raw)

在 16b 之前你可以这样做:

cellfun('length',raw)

不完全清楚您要解决的问题,但您可以考虑使用逻辑索引来分配值:

raw(strcmp(raw,'open')) = {'some value'};
raw(strcmp(raw,'close')) = {pi}

【讨论】:

  • 谢谢@matlabbit!我有 Matlab R2015a。起初它不起作用,因为我不断收到错误“cellfun 仅适用于单元格。”。因此,我首先使用 mat2cell 将其转换为单元格和使用的 cellfun 长度。但后来我得到了所有的 5 分!我想我现在非常非常接近!我再次编辑了我的问题,更好地解释了我的所作所为!非常感谢您的帮助,因为我认为我已经接近最终结果了!
  • 您可以发布示例数据和所需的输出吗?我很难根据对您不起作用的代码来跟踪您尝试解决的问题。
  • 很抱歉,@matlabbit!我附上了一些图片并编辑了一些我尝试过的新东西,它现在给了我两个字符串“open”和“close”的数字——而不是它们的长度(这是我想要的)。
猜你喜欢
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
相关资源
最近更新 更多