Array weirdness

2 次查看(过去 30 天)
Trader
Trader 2012-4-3
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

采纳的回答

Geoff
Geoff 2012-4-3
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 个评论
Trader
Trader 2012-4-3
Thanks for you help, (end+1,:) cleans things up a bit.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by