How to store values from a loop?

7 次查看(过去 30 天)
I have a code that loops through multiple DTA text files and graphs them together. I want to save the average value of V_f from each iteration but am currently stuck becuase it keeps overwriting and only saves the average V_f from the last DTA file.
clear all; clc; close all;
[filename,pathname]=uigetfile('*.DTA','MultiSelect','on');
numCPs=length(filename);
if numCPs>10
numCPs=1;
end
figure
ax=axes;
ax.Box='on';
ax.LineWidth=1.5;
ax.XAxis.FontSize=20; ax.XLabel.FontSize=20;
ax.YAxis.FontSize=20; ax.YLabel.FontSize=20;
hold on
for count=1:1:numCPs
if numCPs>1
CPdata=importdata(fullfile(pathname,filename{count}),' ');
else
CPdata=importdata(fullfile(pathname,filename),' ');
end
temp=strsplit(CPdata{15});
endIndex=length(CPdata);
for i=1:length(CPdata)
temp=strsplit(CPdata{i});
if temp{1}=="CURVE"
startIndex=i+3;
end
endIndex=i-1;
end
data=[];
for i=startIndex:endIndex
temp=strsplit(CPdata{i});
data=[data;str2num(temp{3}),str2num(temp{4})];
end
Time=data(:,1); %time
V_f=data(:,2); %ohm
V_favg=mean(data(:,2));
if numCPs>1
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename{count});
else
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename);
end
xlabel("Time (s)"); ylabel("V_f (V vs. Ref.)");
end
legend('AutoUpdate','off');
axis square;
axis([0 7200 0 10])
hold off
  1 个评论
Stephen23
Stephen23 2023-6-14
Note that you can replace this fragile code:
numCPs=length(filename);
if numCPs>10
numCPs=1;
end
..
for count=1:1:numCPs
if numCPs>1
CPdata=importdata(fullfile(pathname,filename{count}),' ');
else
CPdata=importdata(fullfile(pathname,filename),' ');
end
..
end
with this simpler and much more robust code:
filename = cellstr(filename);
..
for count = 1:numel(filename)
CPdata = importdata(fullfile(pathname,filename{count}),' ');
..
end

请先登录,再进行评论。

采纳的回答

Sanskar
Sanskar 2023-6-14
From your question I got that you want to get all the average value but it overwrites the previous one.
Since, in the following line you are updating the V_favg in every iteration so it overwrites.
V_favg=mean(data(:,2));
To store the average value, you can create an empty 1-D array, before you start the loop and you can append the average value from every iteration to that array.
....
ax.LineWidth=1.5;
ax.XAxis.FontSize=20; ax.XLabel.FontSize=20;
ax.YAxis.FontSize=20; ax.YLabel.FontSize=20;
hold on
V_favg = []; % initialize empty array
for count = 1:1:numCPs
...
...
Time=data(:,1); %time
V_f=data(:,2); %ohm
V_favg(end+1)=mean(data(:,2)); %Appending to initialized array
if numCPs>1
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename{count});
else
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename);
end
xlabel("Time (s)"); ylabel("V_f (V vs. Ref.)");
...
...
end
Happy to help you,
Sanskar

更多回答(2 个)

Aman
Aman 2023-6-14
Hi Edward,
To save the average V_f value from each DTA file, you can create an array V_favg_all to store the average values.
Here's an updated version of your code that includes this modification:
clear all; clc; close all;
[filename,pathname]=uigetfile('*.DTA','MultiSelect','on');
numCPs=length(filename);
if numCPs>10
numCPs=1;
end
figure
ax=axes;
ax.Box='on';
ax.LineWidth=1.5;
ax.XAxis.FontSize=20; ax.XLabel.FontSize=20;
ax.YAxis.FontSize=20; ax.YLabel.FontSize=20;
hold on
V_favg_all = []; % initialize array to store average V_f values
for count=1:1:numCPs
if numCPs>1
CPdata=importdata(fullfile(pathname,filename{count}),' ');
else
CPdata=importdata(fullfile(pathname,filename),' ');
end
temp=strsplit(CPdata{15});
endIndex=length(CPdata);
for i=1:length(CPdata)
temp=strsplit(CPdata{i});
if temp{1}=="CURVE"
startIndex=i+3;
end
endIndex=i-1;
end
data=[];
for i=startIndex:endIndex
temp=strsplit(CPdata{i});
data=[data;str2num(temp{3}),str2num(temp{4})];
end
Time=data(:,1); %time
V_f=data(:,2); %ohm
V_favg=mean(data(:,2));
V_favg_all = [V_favg_all V_favg]; % append the new V_favg to the end of the V_favg_all array
if numCPs>1
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename{count});
else
plot(Time,V_f,'-o','MarkerSize',2,'LineWidth',3,'DisplayName',filename);
end
xlabel("Time (s)"); ylabel("V_f (V vs. Ref.)");
end
legend('AutoUpdate','off');
axis square;
axis([0 7200 0 10])
hold off
The average of each iteration is stored in the V_favg_all array.
Hope this Helps!

Naman
Naman 2023-6-14
Hi Edward,
Here you are overriding the V_favg value with every iteration of for loop
V_favg=mean(data(:,2));
Instead create an array V_favg initially outside the for loop.
% Declaring V_favg2 vector outside the for loop.
V_favg = zeros(1,numCPs) ;
and keep storing average of V_f in it with each iteration of for loop.
% saving the average value of V_f from each iteration into V_favg array
V_favg(count) = mean(data(:,2)) ;
Hope it will help you.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by