Array weirdness

1 visualizzazione (ultimi 30 giorni)
Trader
Trader il 3 Apr 2012
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

Risposta accettata

Geoff
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 };
  1 Commento
Trader
Trader il 3 Apr 2012
Thanks for you help, (end+1,:) cleans things up a bit.

Accedi per commentare.

Più risposte (0)

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!

Translated by