Why isn't my For Loop working?

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 Kommentare

hello
your code works even if yes preallocating will speed up especially if you have large data sets
clc
clearvars
novLat1 =[
-56.63;
-56.50;
-56.43;
-56.27;
-56.17];
novLat2 =[
-56.26;
-56.25;
-56.32;
-56.32;
-56.42];
Lat_longaxis_N01 = zeros(length(novLat1),2); % preallocating for faster execution
for ii = 1:length(novLat1)
Lat_longaxis_N01(ii,:) = [novLat1(ii) novLat2(ii)];
end
% gives :
%
% Lat_longaxis_N01 =
%
% -56.6300 -56.2600
% -56.5000 -56.2500
% -56.4300 -56.3200
% -56.2700 -56.3200
% -56.1700 -56.4200
I believe the trouble is maybe before that loop
have you checked that the lat and long vector data are correctly loaded in the workspace ?
what do you get in the command window when you type who
Mairead Smith
Mairead Smith am 12 Mär. 2021
Oh it's good to know it's almost correct.
I think I have them loaded correctly, when I type who I see:
novLat1 novLon1 novDOY novLat2 novLon2
good news
sorry, I mean : type whos (with an s) in the command window to see the type / size of your data in the workspace like :
>> whos
Name Size Bytes Class Attributes
a 1x50 400 double
Oh right sorry, here you go:
>> whos
Name Size Bytes Class Attributes
Lat_longaxis_N01 31x2 496 double
Lon_longaxis_N01 31x2 496 double
NOVCLIM_sss_ROI 28x72 16128 double
SMAP_lat_NOVROI 28x1 224 double
SMAP_lon_NOVROI 72x1 576 double
ii 1x1 8 double
novDOY 31x1 248 double
novLat1 31x1 248 double
novLat2 31x1 248 double
novLon1 31x1 248 double
novLon2 31x1 248 double
Mairead Smith
Mairead Smith am 12 Mär. 2021
Bearbeitet: Mairead Smith am 12 Mär. 2021
Does it matter that my column vectors (e.g. novLat1) are 31x1, but the final number is a NaN?
Shoudn't be - here I added NaN at the end of both data
and skipped the unnecessary for loop as suggested by Jan (good point)
clc
clearvars
novLat1 =[
-56.63;
-56.50;
-56.43;
-56.27;
-56.17
NaN];
novLat2 =[
-56.26;
-56.25;
-56.32;
-56.32;
-56.42
NaN];
Lat_longaxis_N01 = [novLat1(:) novLat2(:)];
% gives
%
% Lat_longaxis_N01 =
%
% -56.6300 -56.2600
% -56.5000 -56.2500
% -56.4300 -56.3200
% -56.2700 -56.3200
% -56.1700 -56.4200
% NaN NaN
Would you send your own data as a mat file ? - simply do this :
% Save all variables from the workspace to test.mat:
save test.mat
Mathieu NOE
Mathieu NOE am 15 Mär. 2021
hello !
problem solved ?
Mairead Smith
Mairead Smith am 15 Mär. 2021
Hi Mathieu sorry for the delay! Problem still not solved unfortunately.
I have gotten a little further:
I think the error message that was displayed before was to do with something else in the script not working (as I think you and Jan also suggested). I've commented everything else out, and now there is no error message when I run this section.
When I run the section shown I get two new variables which is a start.
Lat_longaxis_N01 and Lon_longaxis_N01.
But I don't get exactly what I want, which is for the loop to move through the rows sequentially and create these pairs of variables as above but for each subsequent row in the columns.
i.e.
Lat_longaxis_N02 and Lon_longaxis_N02
Lat_longaxis_N03 and Lon_longaxis_N03
Lat_longaxis_N04 and Lon_longaxis_N04
etc... down to 30.
How do I edit the loop so it performs like this? This is what loops are supposed to do right?
I will save the mat files and send to you also.
Thanks in advance.
Mairead Smith
Mairead Smith am 15 Mär. 2021
Here is the workspace :)
Jan
Jan am 15 Mär. 2021
"How do I edit the loop so it performs like this?"
Performs like what?
Mairead Smith
Mairead Smith am 15 Mär. 2021
Bearbeitet: Mairead Smith am 15 Mär. 2021
Performs such that it gives me a set of 60 new variables rather than just two variables.
When my loop works as I wish it to, it will create 60 new variables when I run it. It will do this by looping through the rows in my 4 column vectors (30 x 1) as shown in the code below, creating a new pair of variables for each row (each day), numbered Lat_longaxis_N02, N03, N04 etc...
Right now it is giving me just two variables when I run the loop.
Lat_longaxis_N01 = zeros(length(novLat1),2);
Lon_longaxis_N01 = zeros(length(novLon1),2);
for ii = 1:length(novLat1);
Lat_longaxis_N01(ii,:) = [novLat1(ii) novLat2(ii)];
Lon_longaxis_N01(ii,:) = [novLon1(ii) novLon2(ii)];
end
If this is a stupid question or I haven't explained it properly, it's simply because I'm new to Matlab and still learning so you'll have to excuse that. Let me know if you have any advice that would help me at all. Thanks!
Ok
so this is the idea of my code but of course I could test it only for the first iteration
you have all the 30 files to test further :
hope it helps
clc
clearvars
load('test.mat');
clear Lat_longaxis_N01 Lon_longaxis_N01
%%%%% main loop %%%%%%%%%%%%
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(:) novLat2(:)]; % goes with : Lat_longaxis_N01 / 02 / 03 ...
data2 = [novLon1(:) novLon2(:)]; % goes with : Lon_longaxis_N01 / 02 / 03 ...
eval([FileName1 '= data1;']);
eval([FileName2 '= data2;']);
end
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 am 15 Mär. 2021
ok
glad I could be of some help !
Good luck for the future !

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 12 Mär. 2021
Bearbeitet: Jan am 12 Mär. 2021

0 Stimmen

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 Kommentare

Mairead Smith
Mairead Smith am 12 Mär. 2021
Here is the complete error message:
Error: File: CODE_iceberg_trajectory_create_daily_vars.m Line: 69 Column: 26
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
Okay yes it must be a problem elsewhere in the code. Unless I've not explained what exactly I want my loop to do (see below). I just tried copying the specific segment of code into a blank script - it worked, in that it created two new variables.
But.. the reason I want to use a loop is because I want it to run down the rows and create lots of new variables. Eventually, once I gather my data, each column vector will be hundreds of rows. Each row represents daily data. I want this loop to combine each sequential row of the first and second columns, to form a new variable each time for each day. Each new variable should be called 'Lat_longaxis_N01', 'Lat_longaxis_N02', 'Lat_longaxis_N03', etc. For the 1st, 2nd, 3rd Nov.
Apologies if I haven't explained it that well. Does this make sense?
Jan
Jan am 15 Mär. 2021
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 am 15 Mär. 2021
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 12 Mär. 2021

Kommentiert:

am 15 Mär. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by