Why isn't my For Loop working?
Ältere Kommentare anzeigen
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
Mathieu NOE
am 12 Mär. 2021
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
am 12 Mär. 2021
Mathieu NOE
am 12 Mär. 2021
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
Mairead Smith
am 12 Mär. 2021
Mairead Smith
am 12 Mär. 2021
Bearbeitet: Mairead Smith
am 12 Mär. 2021
Mathieu NOE
am 12 Mär. 2021
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
Mathieu NOE
am 12 Mär. 2021
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
am 15 Mär. 2021
hello !
problem solved ?
Mairead Smith
am 15 Mär. 2021
Mairead Smith
am 15 Mär. 2021
Jan
am 15 Mär. 2021
"How do I edit the loop so it performs like this?"
Performs like what?
Mairead Smith
am 15 Mär. 2021
Bearbeitet: Mairead Smith
am 15 Mär. 2021
Mathieu NOE
am 15 Mär. 2021
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
Mairead Smith
am 15 Mär. 2021
Mathieu NOE
am 15 Mär. 2021
ok
glad I could be of some help !
Good luck for the future !
Antworten (1)
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
am 12 Mär. 2021
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
am 15 Mär. 2021
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!