从双重转换到单元格是不可能的- MATLAB

时间:2022-12-27 16:10:44

Is there a way of reading a text file to a cell array of strings and write it into excel file ?

是否有一种方法可以将文本文件读入字符串的单元数组并将其写入excel文件?

i try something like this :

我试着这样做:

for d = 1:n
dFile(d,1) = d
content = strcat(dirpathstr{d})
filecontent =textscan(content, '%s', 'whitespace', '')
dFile(d,2) = filecontent
end

but it give me error "Conversion to cell from double is not possible", then i improve it with

但它给我的错误“从双元转换到单元格是不可能的”,然后我改进了它

"dFile(d,2) = str2double (filecontent)" 

but its give me 'NaN' as result,

但它给了我NaN,

can anyone help me :)

谁能帮帮我?

1 个解决方案

#1


1  

The variable dFile is a double array, i.e. it only contains numbers. The first column always contains d, which is a number - that's fine.

变量dFile是一个双数组,即它只包含数字。第一列总是包含d,这是一个数字,没关系。

The textscan function however returns a cell array. With the line

但是,textscan函数返回一个单元格数组。与行

dFile(d,2) = filecontent

you try to take the cell array and save it inside the double array. This is not possible, as they are different data types. That is exactly what MATLAB tells you:

您尝试获取单元格数组并将其保存到双数组中。这是不可能的,因为它们是不同的数据类型。这就是MATLAB告诉你的

Conversion to cell from double is not possible

从double转换到cell是不可能的

In your case, you don't need dFile to be a double array, you can just make it a cell array too. Then you can assign filecontent to dFile, as they are of the same data type.

在你的例子中,你不需要dFile成为一个双数组,你也可以把它变成一个单元数组。然后可以将filecontent分配给dFile,因为它们具有相同的数据类型。

for d = 1:n
    dFile{d,1} = d
    content = strcat(dirpathstr{d})
    filecontent =textscan(content, '%s', 'whitespace', '')
    dFile{d,2} = filecontent
end

#1


1  

The variable dFile is a double array, i.e. it only contains numbers. The first column always contains d, which is a number - that's fine.

变量dFile是一个双数组,即它只包含数字。第一列总是包含d,这是一个数字,没关系。

The textscan function however returns a cell array. With the line

但是,textscan函数返回一个单元格数组。与行

dFile(d,2) = filecontent

you try to take the cell array and save it inside the double array. This is not possible, as they are different data types. That is exactly what MATLAB tells you:

您尝试获取单元格数组并将其保存到双数组中。这是不可能的,因为它们是不同的数据类型。这就是MATLAB告诉你的

Conversion to cell from double is not possible

从double转换到cell是不可能的

In your case, you don't need dFile to be a double array, you can just make it a cell array too. Then you can assign filecontent to dFile, as they are of the same data type.

在你的例子中,你不需要dFile成为一个双数组,你也可以把它变成一个单元数组。然后可以将filecontent分配给dFile,因为它们具有相同的数据类型。

for d = 1:n
    dFile{d,1} = d
    content = strcat(dirpathstr{d})
    filecontent =textscan(content, '%s', 'whitespace', '')
    dFile{d,2} = filecontent
end