Set number of rows in output variable using indexing within for loop, add variable to new table
Show older comments
I have a table with 74 rows and 5 columns/variables. I'm attempting to use indexing and a for loop to subtract a calculated value from rows in a column that match an identifier code which I have put in an adjacent column. I'm running into a problem where the code+variable matches are not the same number of rows as my original table, which I want to add the newly calcualted columns to. I tried the if - size - end loop but all that did was add zeros to the bottom of the rows to make 74, but I need the values to remain in the same order as the input variable. I would like to put the two calculated variable columns directly after the variable column in the original table. Please see attached. Thank you!
Tab=readtable('D021524.xlsx',VariableNamingRule='preserve');
ID = ["DMCQ","TGA","GMLC","WGG","RSVG","MQ","DMS","CRG","CS","GMLCV"];
Beta=[0.58;1]
T=[573.15;1173.15]
Temp=T
BDplg_w1 = (2.91-0.76.*Beta(:,1)) .* (1e6./T.^2) - 3.41 - 0.41.*Beta(:,1); % Fractionation plag-water OT1967
% %Subtable:
idx = matches(Tab.GroupID,ID)
out = Tab(idx,["GroupID","δ18O WR","δ18O WRr1","δ18O WRr2"]);
for ii = 1:numel(ID(:,:))
ix = (matches(out.GroupID,'DMCQ'))
wr1 = out{ix,"δ18O WR"} - BDplg_w1(1,1);
wr2 = out{ix,"δ18O WRr1"} - BDplg_w1(1,1);
%
% if size(wr1,1) < 74 % this didn't work
% wr1(74,1) = 0;
% end
out{ix,"H2O in EQ"} = wr1;
out = addvars(out,wr1, 'After','δ18O WR')
end
6 Comments
Stephen23
on 16 Feb 2025
Preallocate the entire column, add data to it using indexing (optionally on each loop iteration), then assign it to the table.
tbl = readtable('D021524.xlsx', VariableNamingRule='preserve');
tbl(:,"Var"+digitsPattern) = []
% The following three vectors must have the same number of elements:
ID = ["DMCQ"; "TGA"];
Beta = [ 0.58; 1];
T = [573.15;1173.15];
BDplg_w1 = (2.91-0.76.*Beta(:,1)) .* (1e6./T.^2) - 3.41 - 0.41.*Beta(:,1); % Fractionation plag-water OT1967
wr0 = nan(height(tbl),1); % preallocate
wr1 = nan(height(tbl),1); % preallocate
for ii = 1:numel(ID)
idx = matches(tbl.GroupID,ID(ii));
wr0(idx) = tbl{idx,"δ18O WR"} - BDplg_w1(ii);
wr1(idx) = tbl{idx,"δ18O WRr1"} - BDplg_w1(ii);
end
tbl = addvars(tbl,wr0, 'After','δ18O WR');
tbl = addvars(tbl,wr1, 'After','δ18O WRr1');
display(tbl)
If you have more than two columns/variables that you need to process in this way then use a loop for those too.
Erin Summerlin-Donofrio
on 16 Feb 2025
Erin Summerlin-Donofrio
on 16 Feb 2025
"Is this because of how the code is written (and its too complicated or can't be done for more than two columns/variables) or for some other reason?"
Of course it can be done. The reason I would not is that I am lazy (and you should be too). Computers are really only good at one thing: repeating simple tasks in loops. So when you copy-and-paste (and then tweak) lines of almost-identical code then you are just doing the computer's job for it.
If you can easily identify the IDs (either by writing a short list or automatically using e.g. PATTERN matching on the table variable names) then you can easily loop over them using indexing. It probably requires an outer nested loop, possibly vectors of the corresponding parameters, and some indexing (i.e. very basic MATLAB stuff).
Of course there is nothing stopping you from doing the computer's work for it by copy-and-pasting lines of code for every ID that you want to process. Note however that:
- more code increases the chances of bugs
- more code means bugfixing/maintanenance in more locations (i.e. it is simpler to modify one line in a nested loop than fifty almost-identical lines of code that you have laboriously copy-and-pasted).
It depends on what you are doing: writing generalised code relies on identifying patterns in the data that you are processing and making use of them. For example, look at the lines of code that define/process WR0 and WR1, they are really all the same, the only differences are the IDs themselves. So rather than copy-and-pasting more of those lines add an outer loop and iterate over those IDs!
This also makes your code more generalised: later when your run your code on a different file with slightly different IDs you only need to change one list of IDs (or, if you can identify them from the file data, change nothing at all), i.e. you would not need to go through and change all of your code.
Accepted Answer
More Answers (1)
I would like to put the two calculated variable columns directly after the variable column in the original table.
And you want new columns added with every pass through the loop? That sounds like a strange thing to do, but if you insist it's what you want, then...
miss=nan(height(out),1);
for ii = 1:numel(ID(:,:))
ix = (matches(out.GroupID,'DMCQ'));
wr1 = out{ix,"δ18O WR"} - BDplg_w1(1,1);
wr2 = out{ix,"δ18O WRr1"} - BDplg_w1(1,1);
out{ix,"H2O in EQ"} = wr1;
out = addvars(out,miss,miss, 'After',3);
out{ix,4:5}=[wr1,wr2];
end
2 Comments
Erin Summerlin-Donofrio
on 16 Feb 2025
Matt J
on 16 Feb 2025
I still don't really understand what the loop is supposed to be doing. You never use the loop variable ii anywhere.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!