下标分配尺寸不匹配. Matlab中的错误 [英] Subscripted assignment dimension mismatch. error in matlab

查看:102
本文介绍了下标分配尺寸不匹配. Matlab中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    for i=1:30
  Name(i,1)=sprintf('String_%i',i);
end

我只是对这里不起作用的内容感到困惑,该脚本看起来非常简单,需要创建一个编号从1到30的字符串列表.

I'm just confused what is not working here, this script seems very straightforward, wnat to build a list of strings with numbering from 1 to 30. getting error

订阅的分配维度不匹配.

Subscripted assignment dimension mismatch.

推荐答案

Matlab确实没有字符串,它们具有char数组.就像几乎所有编程语言一样,Matlab在不知道要分配多少内存的情况下也无法定义变量. Java解决方案如下所示:

Matlab do not really have strings, they have char arrays. As in almost any programming language Matlab cannot define a variable without knowing how much memory to allocate. The java solution would look like this:

String str[] = {"I","am","a","string"};

类似于c ++解决方案:

Similar to the c++ solution:

std::string str[] = {"I","am","another","string"};

c解决方案看起来有所不同,但通常与c ++中的解决方案相同:

The c solution looks different, but is generally the same solution as in c++:

const char* str[] = {"I","am","a","c-type","string"};

但是,尽管看起来很像,但它们在本质上是相同的,即使它们不会启动,它们都知道要分配多少数据.特别地,您可以例如编写:

However, despite the appearances these are all fundamentally the same in the sense to that they all knows how much data to allocate even though they would not be initiated. In particular you can for example write:

String str[3];
// Initialize element with an any length string.

原因是存储在每个元素中的内存是通过java中的引用以及c和c ++中的指针存储的.因此,取决于操作系统,每个元素都是4(32位)或8(64位)字节.

The reason is that the memory stored in each element is stored by its reference in java and by a pointer in c and c++. So depending on operating system, each element is either 4 (32-bit) or 8 (64-bit) bytes.

但是,在Matlab矩阵中,数据是按值存储的.这使得不可能将N char数组存储在1xNNx1矩阵中.矩阵中的每个元素仅允许与char大小相同,并且类型为char.这意味着,如果您使用字符串,则需要使用数据结构cell(如 Benoit_11 所建议的那样) ),它在每个元素中存储了对任何Matlab对象的引用.

However, in Matlab matrices data is stored by value. This makes it impossible to store a N char arrays in a 1xN or Nx1 matrix. Each element in the matrix is only allowed to be of the same size as a char and be of type char. This means that if you work with strings you need to use the data structure cell (as also suggested by Benoit_11) which stores a reference to any Matlab object in each element.

k = 1:30;
Name = cell(length(k),1);
for i=k
    Name{i,1}=sprintf('String_%i',i);
end

希望说明对您有意义.我假设根据您的尝试,您至少具有至少一种不同于matlab的语言的编程经验.

Hope that the explanation makes sense to you. I assumed that according to your attempt you have at least some programming experience from at least one other language than matlab.

这篇关于下标分配尺寸不匹配. Matlab中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆