Why isn't my For Loop working?

조회 수: 1 (최근 30일)
Mairead Smith
Mairead Smith 2021년 3월 12일
댓글: Mathieu NOE 2021년 3월 15일
Hi all,
I am new to Matlab, and am struggling currently to write a functional For Loop.
If anyone could point me to where I am going wrong/what I'm missing, I would be so grateful!
I have 2 column vectors, containing lat and long positions. Sample below:
novLat1 novLat2
-56.63 -56.26
-56.50 -56.25
-56.43 -56.32
-56.27 -56.32
-56.17 -56.42
I need to extract the data from each row of novLat1 and novLat2 to make a new simple two-element vector variable, e.g. [-56.63 -56.26].
Here is my (very basic) code so far:
for ii = 1:length(novLat1);
Lat_longaxis_N01(ii,:) = [novLat1(ii) novLat2(ii)];
end
and the error message I am seeing:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
I have no idea why this isn't working. The error message when I hover over the code says: 'The variable Lat_longaxis_N01' appears to change size on every loop iteration. Consider preallocating for speed.'
Thanks in advance.. if I can get this working it will save me so much time as there are many, many lat and long positions to work through.
  댓글 수: 15
Mairead Smith
Mairead Smith 2021년 3월 15일
Whoa it works!! Thank you so much!
This is exactly what I was looking for. I didn't know how to set the base names like you have done here but knew in theory I would have to do something like this, so that's perfect.
I had to make one little tweak, just editing the data1 and data2 lines so they indexed the single row at where the counter sits (rather than the whole column), see below, but now it runs smoothly and the data looks good.
Thanks again Mathieu that's so helpful! :)
BaseName1='Lat_longaxis_N';
BaseName2='Lon_longaxis_N';
for k=1:30
if k<10
str = ['0' num2str(k)];
else
str = num2str(k);
end
FileName1=[BaseName1,str]; % example : Lat_longaxis_N01 / 02 / 03 ...
FileName2=[BaseName2,str]; % example : Lon_longaxis_N01 / 02 / 03 ...
data1 = [novLat1(k,:) novLat2(k,:)]; % goes with : Lat_longaxis_N01 / 02 / 03 ...
data2 = [novLon1(k,:) novLon2(k,:)]; % goes with : Lon_longaxis_N01 / 02 / 03 ...
eval([FileName1 '= data1;']);
eval([FileName2 '= data2;']);
end
Mathieu NOE
Mathieu NOE 2021년 3월 15일
ok
glad I could be of some help !
Good luck for the future !

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2021년 3월 12일
편집: Jan 2021년 3월 12일
Please post a copy of the complete message. This includes the line number.
The posted code does not contain a problem which produce this error message. So I guessd boldly, that it is another part of the code. Maybe you did not save the file after editing? Or this is not the file, which is actually running? Check this using a breakpoint in the code.
By the way, there is no need for a loop:
Lat_longaxis_N01 = [novLat1, novLat2];
  댓글 수: 3
Jan
Jan 2021년 3월 15일
The message tells you, that the problem is here:
CODE_iceberg_trajectory_create_daily_vars.m Line: 69 Column: 26
So what do you find in this line? Did you fix it already?
Mairead Smith
Mairead Smith 2021년 3월 15일
Yep thanks Jan, the error message isn't displaying anymore as yes, I have fixed the error on Line 69 Column 26. See above for an explanation of how my loop still isn't working as I had hoped :(
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by