필터 지우기
필터 지우기

(MATLAB beginner) How can I solve this error: In an assignment A(:) = B, the number of elements in A and B must be the same.

조회 수: 1 (최근 30일)
I'm trying to use MATLAB for my SWMM project in calibrating certain parameters. Shown below was the part of the program:
runvol_mea=[2.071
];
RunVol_mea=runvol_mea(1,:);
peakf=[0.122
];
PeakF_mea=peakf(1,:);
% Upper and Lower boundaries of parameters
lb = [250 60 0.9];
ub = [350 80 1.4];
% populate initial starting vertices (6 vertices) k = 2n ; n=3 (number of parameters to be calibrated)
par_1=zeros(6,3);
for i=1:6
par_o(i,:)=lb+rand(1,3).*(ub-lb);
end
% Model run
par=par_o;
max_int=300; % maximum number of iteration
Toler_=zeros(10,3); % Counter check
RunVol_sim=zeros(1,6); % number of vertices / number of storms
PeakF_sim=zeros(1,6);
E=zeros(1,1);
for i=1:6 % for 6 different vertices
%i
% Changing Subcatchment Area
Data{1,1}{index3+1}(52:58)=sprintf('%6.4f',par(i,1)); (This part was the error)
% Changing % impervious
Data{1,1}{index3+2}(59:61)=sprintf('%2f',par(i,2));
% Changing Subcatchment width range
Data{1,1}{index3+4}(62:67)=sprintf('%5.2f',30.72*par(i,3));
In the command window, it shows,
??? In an assignment A(:) = B, the number of elements in A and B
must be the same.
Error in ==> Tbftest at 60
Data{1,1}{index3+1}(52:58)=sprintf('%6.4f',par(i,1));** *
Its my first time using MATLAB and my seniors doesn't know how to handle this as well. I really appreciate anyone's help on this.

채택된 답변

Iain
Iain 2014년 2월 12일
Ick. Data is a cell array that contains a cell array that contain arrays.
Data{1,1}{index3+1}(52:58) is 7 characters long.
sprintf('%6.4f',par(i,1)); is a different length. It is at least 6 characters long.
Obviously, you can't fit 6, 8, 9 or 10 characters in a 7 character long space, so you need to adapt either the space you're sticking it in, or change what you're trying to stick in.
string_to_add = sprintf('%6.4f',par(i,1));
length_of_string = numel( string_to_add);
Data{1,1}{index3+1}(52:(52+length_of_string-1)) = string_to_add;
That'll get rid of the error, but not necessarily fix what the problem is.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2014년 2월 12일
52:58 is seven locations, but your 6.4f format is producing six characters.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by