Filter löschen
Filter löschen

Code overwrites results in for loop. Cannot figure out how to index.

1 Ansicht (letzte 30 Tage)
I've run into a problem with the following code, which is part of a for loop with index k.
if strcmp(l(1:5),'alpha') %Get row starting with alpha
l = fgetl(fid); % get next line
first_data = str2num(l);
alpha_beta = [first_data(1) first_data(3)]; % store alpha and beta
end
My issue is that the alpha_beta variable keeps overwriting with each iteration of the for loop. Ideally I'd like to save first_data(1) and first_data(3) from each iteration in alpha_beta. I tried a few things with indexing including
alpha_beta{k} = [first_data(1) first_data(3)];
but because it's a 'double' it won't allow brace indexing. I can't seem to find any alternatives, so if anyone has a suggestion it would be much appreciated.

Akzeptierte Antwort

Stephen23
Stephen23 am 20 Nov. 2019
Either
alpha_beta(k,1) = first_data(1);
alpha_beta(k,2) = first_data(3);
or
alpha_beta(k,1:2) = first_data([1,3]);
And remeber to preallocate alpha_beta before the loop:

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by