Array weirdness
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone, I have something very strange happening that I don't understand...
I'm using a for loop to fill an array with values, the first iteration works just fine, the second works correctly but 16 blank rows appear before my second iteration is written. The blank cells have the same number of columns as the first and second. It's only between the first and second row that this happens. Any ideas?
Here's basically what I have gong on:
results.full_data = {};
obsv = 1;
for 20:1:400
... fill struct met that will hold the data going into results.full_data ...
[results] = buildOutput(counter, met, results);
obsv = obsv+1;
end
function[results] = buildOutput(obsv, met, results)
z = length(results.full_data)+1;
results.full_data(z,:) = {met.counter(obsv) met.current_date(obsv) met.current_time(obsv) met.action(obsv) met.position(obsv) met.pa(obsv) met.bp(obsv) met.num_shares(obsv) met.entry_price(obsv) met.price(obsv) met.sma(obsv) met.stdev(obsv) met.t_band(obsv) met.tm_band(obsv) met.l_band(obsv) met.lm_band(obsv) met.profit(obsv)};
end
Any advice you'd be able to give would be greatly appreciated
Thank you
0 Commenti
Risposta accettata
Geoff
il 3 Apr 2012
The call to length returns the largest dimension, which in your case is 17. So the first time, you write 1 row, then you write the 18th row. Thus leaving 16 empty rows between.
Instead of length use size(results.full_data,1) to return the number of rows.
Note that you could have dispensed with the 'z' calculation altogether and done this:
results.full_data(end+1,:) = { etc etc etc };
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!