Set number of rows in output variable using indexing within for loop, add variable to new table
5 views (last 30 days)
Show older comments
Erin Summerlin-Donofrio
on 16 Feb 2025
Answered: Siddharth Bhutiya
on 20 Feb 2025
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
Edited: Stephen23
on 17 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
Siddharth Bhutiya
on 20 Feb 2025
Based on the description it looks like you have one value of BDplg_w1 for each unique GroupID. If that is the case then instead of using indexing to find matching rows in the table, you can instead use a dictionary that maps a GroupID to the corresponding BDplg_w1 value. So something like the following
tbl = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1826322/D021524.xlsx", VariableNamingRule='preserve');
tbl(:,"Var"+digitsPattern) = [];
ID = ["DMCQ","TGA","GMLC","WGG"];
Beta = [ 0.58; 1; 0.58; 1];
T = [573.15;573.15;573.15;573.15;];
val = (2.91-0.76.*Beta(:,:)) .* (1e6./T.^2) - 3.41 - 0.41.*Beta(:,:); % Fractionation plag-water OT1967
BDplg_w1 = dictionary(ID',val)
Now you can use the group id vector from your table to lookup BDplg_w1 values for each group id. You can also specify NaN as your fallback value, so if a group is missing from your dictionary it will be populated with a NaN.
WR0 = tbl.("δ18O WR") - lookup(BDplg_w1, tbl.GroupID, FallBackValue=NaN);
tbl1 = addvars(tbl,WR0,After="δ18O WR")
If you can somehow programmatically get all your desired variable names, then you can do something like this to calculate and add these values to your table in a loop.
vars = tbl.Properties.VariableNames;
vars = vars(startsWith(vars,"δ"))
for i = 1:numel(vars)
var = vars{i};
wr = tbl.(var) - lookup(BDplg_w1, tbl.GroupID, FallBackValue=NaN);
tbl = addvars(tbl, wr, After=var, NewVariableNames="WR"+i);
end
tbl
Hope this helps!
0 Comments
More Answers (1)
Matt J
on 16 Feb 2025
Edited: Matt J
on 16 Feb 2025
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
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.
See Also
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!