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 个评论
dpb
2022-8-26
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
2022-8-26
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
2022-8-30
编辑:James McBrearty
2022-8-30
dpb
2022-8-31
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
2022-8-31
回答(1 个)
Image Analyst
2022-8-26
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'});
类别
在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
