nodes4 = []; for i = 1:length(data) node_str = strsplit(data(i), ': '); node_coord = strsplit(node_str{2}, ','); node_x = str2double(node_coord{1}(2:end)); node_y = str2double(node_coord{2}); node_z = str2double(node_coord{3}(1:end-1)); nodes4 = [nodes4; node_x, node_y, node_z]; end 错误使用 strsplit (line 80) 第一个输入必须为字符向量或字符串标量。 出错 oooo (line 703) node_str = strsplit(data(i), ': ');
时间: 2024-01-31 08:04:28 浏览: 36
这个错误通常是由于 `data` 变量中包含非字符串元素而导致的。你可以通过将其转换为字符串数组来解决这个问题,例如:
```
data = string(data); % 将 data 转换为字符串数组
nodes4 = [];
for i = 1:length(data)
node_str = strsplit(data(i), ': ');
node_coord = strsplit(node_str{2}, ',');
node_x = str2double(node_coord{1}(2:end));
node_y = str2double(node_coord{2});
node_z = str2double(node_coord{3}(1:end-1));
nodes4 = [nodes4; node_x, node_y, node_z];
end
```
在这个例子中,我们首先使用 `string` 函数将 `data` 转换为字符串数组,然后再使用 `strsplit` 函数进行字符串操作,以避免出现上述错误。
相关问题
此类型的变量不支持使用大括号进行索引。 出错 oooo (line 703) node_str = strsplit(data{1}{i}, ': '); nodes4 = []; for i = 1:length(data) node_str = strsplit(data{1}{i}, ': '); node_coord = strsplit(node_str{2}, ','); node_x = str2double(node_coord{1}(2:end)); node_y = str2double(node_coord{2}); node_z = str2double(node_coord{3}(1:end-1)); nodes4 = [nodes4; node_x, node_y, node_z]; end
这个错误是因为 `data` 变量不是一个 cell 数组,不能使用大括号索引。如果 `data` 是一个字符串数组,可以使用以下代码:
```
nodes4 = [];
for i = 1:length(data)
node_str = strsplit(data(i), ': ');
node_coord = strsplit(node_str{2}, ',');
node_x = str2double(node_coord{1}(2:end));
node_y = str2double(node_coord{2});
node_z = str2double(node_coord{3}(1:end-1));
nodes4 = [nodes4; node_x, node_y, node_z];
end
```
这种方法与之前的方法类似,只是将 `data{1}{i}` 替换为 `data(i)`。这样做可以使代码适用于字符串数组。
fid2 = fopen('resultjieshu1.txt', 'r'); data2 = textscan(fid2, '%s', 'Delimiter', '\n'); fclose(fid2); nodes2 = []; for i = 2:length(data2{1}) node_str = strsplit(data2{1}{i}, ','); node_id = str2double(node_str{1}(4:end)); node_x = str2double(node_str{2}(2:end)); node_y = str2double(node_str{3}(2:end)); node_z = str2double(node_str{4}(2:end-1)); nodes2 = [nodes2; node_x, node_y, node_z]; end可以用另一种读取方式吗
是的,可以使用其他方式读取文本文件,例如使用`fscanf`函数读取数据,具体代码如下:
```
fid = fopen('resultjieshu1.txt', 'r');
if fid == -1
error('无法打开文件');
end
data = fscanf(fid, '节点 %d,坐标为 (%f,%f,%f)\n', [4, Inf]);
fclose(fid);
node_id = data(1,:);
node_x = data(2,:);
node_y = data(3,:);
node_z = data(4,:);
nodes2 = [node_x', node_y', node_z'];
```
这段代码使用`fscanf`函数按照指定的格式读取文本文件中的数据,并将结果存储在数组中。其中,`fscanf`函数的第一个参数指定了读取的格式,第二个参数指定了读取的数据的维度。读取到的数据将存储在二维数组`data`中,然后使用索引操作将数据分别存储在不同的变量中。最后,将节点坐标存储在`nodes2`数组中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)