Matlab报错‘Assignment has more non-singleton rhs dimensions...’原因咨询
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:
- You're reusing the
fnvariable for two conflicting purposes: First, you usefnto store a single filename string (like"Bark.1.png", which is a 1×N character array). Then you immediately try to usefnagain as a 2D array, assigning that full string tofn(a,b)— which refers to a single character position in the variable. - Dimension mismatch on assignment:
fn(a,b)targets one specific character slot (sinceaandbare scalar numbers like 1, 2, etc.), but the right-hand sidefnis 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

