How to make a loop inside a loop?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MUHAMMAD FITRI
le 24 Mai 2022
Réponse apportée : Constantino Carlos Reyes-Aldasoro
le 24 Mai 2022
I want to make a coding that for one set of data that consist 12502 datas, it will equal to 0. Then, for the next data it will equal to 4 with the increment of 4 as the code below. But the error came out says that dimension is not
for i = 0:10
filename1 = [folder_name, '\C14km spliced all disp0000', int2str(i), filenameExtension];
tmpData = dlmread(filename1); % Amplitude data
filename2 = ('distance.txt');
tmpData1 = dlmread(filename2); % Distance
E =[tmpData,tmpData1];
dlmwrite('newData.txt', E); % Amplitude with Distance
%save 'newData.mat',E;
OneMatrix = [TempMatrix;E];
TempMatrix = OneMatrix ;
for selaMasa = 1: length(TempMatrix)
tempSela = zeros(12502,1);
tempSela = tempSela + 4;
hold on
end
end
0 commentaires
Réponses (1)
Constantino Carlos Reyes-Aldasoro
le 24 Mai 2022
Difficult to answer without more context, but I suspect that the error is not related to the loops but something else. There are other points that whilst not errors, will not work:
for selaMasa = 1: length(TempMatrix)
tempSela = zeros(12502,1);
tempSela = tempSela + 4;
hold on
end
In this case, every time you enter the loop, you initialise your variable tempSela to zeros, then add four. So, no matter how many times you iterate inside the loop, the answer will always be 4 for every value of tempSela. You could simply do the following:
tempSela = zeros(12502,1) + 4*length(TempMatrix);
In addition, hold on is used when you create a figure, plot something that you want to keep and then plot something else. In this case it is not necessary.
Hope this helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!