Help with appending onto a table in a loop
Afficher commentaires plus anciens
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
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)
sqlTable = T(1,:)
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
dpb
le 26 Août 2022
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
le 30 Août 2022
Modifié(e) : James McBrearty
le 30 Août 2022
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...
James McBrearty
le 31 Août 2022
Réponses (1)
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
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
