Info
此问题已关闭。 请重新打开它进行编辑或回答。
Subscripted assignment dimension mismatch.
1 次查看(过去 30 天)
显示 更早的评论
I am a beginner programmer, but feel like I am missing something pretty fundamental here.
I am trying to make a function that takes 2 variables:
- a string str
- an integer N
It should then make a 1xN matrix containing the input string at each index and output the matrix.
function bigstring = string_multiplication(str,N)
bigstring(1:N) = str;
disp(bigstring);
end
When I call the function in the command window, I get the following message:
>> string_multiplication('Name',5)
Subscripted assignment dimension mismatch.
Error in string_multiplication (line 8)
bigstring(1:N) = str;
I have also tried predefining 'bigstring = zeros(1,N)' and using a for loop and get similar results.
0 个评论
回答(1 个)
Fabio Freschi
2020-4-4
You can use repmat and create a char array
function bigstring = string_multiplication(str,N)
bigstring = repmat(str,N,1);
disp(bigstring);
end
1 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!