Changing variable name within a nested for loop by looping end number in the variable name.

Hello MATLAB community,
Please go throgh the image once, that i have attached.
I want to loop this 'for loop' for 6 cylinders by simply looping PCYL'1' to PCLY'2' and so on such that the variable name itself is changing.
I know two other ways to do this using the 'if' command and changing dataset for every counter until 6 and the other one being copy pasting this 6 times in the same function (not a fan of this).
I understand that my idea might sound absurd but I just wanted to know if this is possible. I tried looking for a similar question but could not find any.
This is the first time I have asked a question on this community, so sorry if I have not followed a specific way to ask a question.
Code:
function [a] = pre_referencing_avg_method(a)
% Input required is the strut file for each sweep. In this case we will
% have 7 files
% THIS FUNCTION IS TO BE USED WHEN MANIFOLD PRESSURE IS CONSTANT
PMAP = 1; %bar
[row] = find(a.PCYL1.axis==-180);
%Pressure referencing cylinder 1
for j = 1:6
for i=1:length(a.PCYL{'j'}.data(1,:))
cycle = a.PCYL1.data(:,i);
avg_pcyl_pre_around_BDC = mean(cycle((row-5):(row+5)));
delta_pre = PMAP - avg_pcyl_pre_around_BDC;
a.referenced_PCYL.CYL{'j'}(:,i)= cycle + delta_pre;
end
end

 Accepted Answer

It is very important to learn the correct terminology for your data, because this makes it much much easier to find information and documentation. Those are actually fields of a structure, they are not separate variables. And accessing different structure fieldnames in a loop is easy, the MATLAB documentation shows how:
Something like this:
function a = pre_referencing_avg_method(a)
% Input required is the strut file for each sweep. In this case we will
% have 7 files
% THIS FUNCTION IS TO BE USED WHEN MANIFOLD PRESSURE IS CONSTANT
PMAP = 1; %bar
row = find(a.PCYL1.axis==-180);
%Pressure referencing cylinder 1
for j = 1:6
fnm = sprintf('PCYL%d',j); % <-----------generate fieldname.
for i=1:length(a.(fnm).data(1,:)) % <--- use dynamic fieldname.
cycle = a.(fnm).data(:,i); % <------ use dynamic fieldname.
avg_pcyl_pre_around_BDC = mean(cycle((row-5):(row+5)));
delta_pre = PMAP - avg_pcyl_pre_around_BDC;
a.referenced_PCYL.(fnm)(:,i)= cycle + delta_pre; % <--- use dynamic fieldname.
end
end

5 Comments

Thank you so much, that worked like a charm. That was so simple, now that I know about it. I wonder how many days it would have taken me to discover "generate field name" and/or the "sprintf" function.
@Harpreet Singh Walia: yes, learning all of the terminology takes time, there is no way to avoid that. But if you want to know something, the volunteers on this forum are always happy to help! You might find this useful too:
@stephen: The link you shared was a good read. Thank you for sharing. Will try and apply those tips in my everyday coding to learn more.
Nota Bene: @Peter Perkins' alternate syntax relies on the overloaded "+" operator for the new(ish) string class...it doesn't work for cellstr or char() strings, but is quite a handy addition.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!