Help with appending onto a table in a loop

Hi All,
I have a bit of code where I am trying to create a table in a loop, appending on as each loop passes. No matter what way I try it, it only shows up with the last iteration, rather than the whole amount. Would anyone be able to help? Here is the code:
sqlTable = table;
sqlTableTemp = table;
% Doing for all non-adjusted
for i = 1:length(numModels)
sqlTableTemp.forecastType = repmat(forecastType,length(periodEnd),1);
sqlTableTemp.forecastName = repmat(forecastName,length(periodEnd),1);
sqlTableTemp.modelName = repmat(modelNames{i}, length(periodEnd),1);
sqlTableTemp.createDate = repmat(createDate,length(periodEnd),1);
sqlTableTemp.createTime = repmat(createTime,length(periodEnd),1);
sqlTableTemp.creator = repmat(creator,length(periodEnd),1);
sqlTableTemp.activeForecast = zeros(length(periodEnd),1);
sqlTableTemp.periodEnding = periodEnd;
sqlTableTemp.val = cell2mat(app.ForecastResultsTable.UserData(:,i+1));
sqlTable = [sqlTable;sqlTableTemp];
end
All comments and help would be appreciated.
Thanks, James

6 commentaires

dpb
dpb le 26 Août 2022
Looks ok so first guess is the data aren't what you think -- either maybe numModels is only 1 or whatever periodEnd is (none of these are defined in the code snippet so we've no klew where they're coming from nor what they are) is turning out to be zero (there's no data in the various forecastXXX variables, iow).
We simply can't tell; no data from which to work; not code even to see how they may have been being defined.
Can you share an your variables? I can't duplicate with this example
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
T = 5×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80
sqlTable = T(1,:)
sqlTable = 1×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93
sqlTableTemp = T(1,:);
% Doing for all non-adjusted
for i = 2:height(T)
sqlTableTemp.LastName = LastName(i);
sqlTableTemp.Age = Age(i);
sqlTableTemp.Smoker = Smoker(i);
sqlTableTemp.Height = Height(i);
sqlTableTemp.Weight = Weight(i);
sqlTableTemp.BloodPressure = BloodPressure(i,:);
sqlTable = [sqlTable;sqlTableTemp];
end
sqlTable
sqlTable = 5×6 table
LastName Age Smoker Height Weight BloodPressure ___________ ___ ______ ______ ______ _____________ {'Sanchez'} 38 true 71 176 124 93 {'Johnson'} 43 false 69 163 109 77 {'Li' } 38 true 64 131 125 83 {'Diaz' } 40 false 67 133 117 75 {'Brown' } 49 true 64 119 122 80
Nor could I with the cruder but more like OP's example with the beginning empty table...
>> sqlT=table;
>> tT=array2table(x);
>> sqlT=[sqlT;tT]
sqlT =
1×5 table
x1 x2 x3 x4 x5
__ __________________ __________________ _________________ _________________
0 0.0473606932701979 0.0947213865403958 0.142082079810594 0.189442773080792
>> sqlT=[sqlT;tT]
sqlT =
2×5 table
x1 x2 x3 x4 x5
__ __________________ __________________ _________________ _________________
0 0.0473606932701979 0.0947213865403958 0.142082079810594 0.189442773080792
0 0.0473606932701979 0.0947213865403958 0.142082079810594 0.189442773080792
>>
I'd always done similar with simply
sqlT=[];
as the placeholder into which to accumulate so just checked there wasn't something funky going on with the original empty table...
James McBrearty
James McBrearty le 30 Août 2022
Modifié(e) : James McBrearty le 30 Août 2022
Hi All,
The variables used are attached. Basically it is a collection of forecasts, i.e. 4 different models, and that's why I have it to loop over them all, and try and join it together. This will be used to be entered into a SQL database, and the original code done this one line at a time, so we are just changing the code to do a bulk upload instead.
It seems to do the first iteration as expected:
However, it seems that it doesn't do anymore iterations.Do you know what the issue is? I'm sure it's something simple.
dpb
dpb le 31 Août 2022
The value of numModels over which you're looping is still undefined with the attached -- as are all the RH side variables given.
We can't even try to duplicate the example code but at least two have illustrated that the catenation would work as expected if the loop runs multiple times.
I'm with @Image Analyst, however, that it's probable the table can be built from the data directly instead, IF we could see what the starting data are instead...
So yeah, it was a bit of an error on my part. numModels was a number that was defined earlier in the source code. I forgot about this, and was trying for find the size of the original array. It was all fixed by changing the original for loop to be
for i=1:numModels
end
All of the code works fine now, and is 250 times faster for sql entry than the original code that was used.
Thanks for all the help everyone

Connectez-vous pour commenter.

Réponses (1)

Image Analyst
Image Analyst le 26 Août 2022
Why not just do
val = cell2mat(app.ForecastResultsTable.UserData(:,i+1)); % Or whatever.
sqlTable = table(forecastType(:), forecastName(:), modelNames(:), createDate(:), ...
createTime(:), creator(:), activeForecast(:), periodEnding(:), val(:),
'VariableNames', {'forecastType', 'forecastName', 'modelNames', 'createDate', ...
'createTime', 'creator', 'activeForecast', 'periodEnding', 'val'});

Catégories

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by