You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matlab报错‘Assignment has more non-singleton rhs dimensions...’原因咨询

分析MATLAB报错:Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Hey there, let's break down exactly why you're hitting this error and how to fix it!

First, let's translate that jargon-heavy error message into plain language: You're trying to shove a multi-element value (like a full filename string) into a spot that can only hold a single character. The right-hand side of your assignment is way bigger than the left-hand side can accommodate, and MATLAB is calling out that dimension mismatch.

Let's pinpoint the problem in your code

Your loop has two critical mistakes that trigger this error:

  1. You're reusing the fn variable for two conflicting purposes: First, you use fn to store a single filename string (like "Bark.1.png", which is a 1×N character array). Then you immediately try to use fn again as a 2D array, assigning that full string to fn(a,b) — which refers to a single character position in the variable.
  2. Dimension mismatch on assignment: fn(a,b) targets one specific character slot (since a and b are scalar numbers like 1, 2, etc.), but the right-hand side fn is the entire 1×N filename string. You can't fit a 10-character string into a 1-character spot — that's exactly what causes the error.

Fixes to get your code working

You need a separate variable to store all your filenames, using a type that can handle multiple strings. Here are three straightforward options:

Option 1: Use a String Array (Recommended for MATLAB R2016b+)

String arrays are built for this use case — they let you store variable-length strings in a clean 2D grid:

prefix = {'Bark', 'Bush', 'Fabric', 'Floor',... 
          'Flower', 'Food', 'Gravel', 'Hair',... 
          'Marble', 'Metal', 'Paint'};
% Initialize a 9×11 string array to hold all filenames
file_names = strings(9, 11);

for a = 1:9
    for b = 1:11
        file_names(a,b) = sprintf('%s.%d.png', prefix{a}, b);
    end
end

Option 2: Use a Cell Array (Works in all MATLAB versions)

Cell arrays let you store any type of data in each cell, including variable-length strings — no need to worry about uniform lengths:

prefix = {'Bark', 'Bush', 'Fabric', 'Floor',... 
          'Flower', 'Food', 'Gravel', 'Hair',... 
          'Marble', 'Metal', 'Paint'};
% Initialize a 9×11 cell array
file_names = cell(9, 11);

for a = 1:9
    for b = 1:11
        file_names{a,b} = sprintf('%s.%d.png', prefix{a}, b);
    end
end

Option 3: Use a Character Array (Only if you need fixed-length strings)

Character arrays require every row to be the same length, so you'll need to pad shorter strings with spaces:

prefix = {'Bark', 'Bush', 'Fabric', 'Floor',... 
          'Flower', 'Food', 'Gravel', 'Hair',... 
          'Marble', 'Metal', 'Paint'};
% Calculate the maximum length of any filename to set the array size
max_filename_length = max(cellfun(@(p) length(sprintf('%s.%d.png', p, 11)), prefix));
% Initialize a 99×max_length character array (flattened from 9×11)
file_names = char(zeros(9*11, max_filename_length));
index = 1;

for a = 1:9
    for b = 1:11
        current_fn = sprintf('%s.%d.png', prefix{a}, b);
        file_names(index, 1:length(current_fn)) = current_fn;
        index = index + 1;
    end
end

Quick Key Takeaways

  • Never reuse the same variable name for two different jobs (a single string vs. a 2D array) — it creates unavoidable dimension conflicts.
  • Always ensure the left and right sides of an assignment match in dimensions, or use a flexible container like a string/cell array to handle variable-length data.

内容的提问来源于stack exchange,提问作者bockdavidson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:28:36