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

 채택된 답변

Stephen23
Stephen23 2020년 11월 3일
편집: Stephen23 2020년 11월 3일
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

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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 11월 3일

댓글:

dpb
2023년 9월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by