How do I store every output from every iteration of a for loop in an array?

조회 수: 4 (최근 30일)
I have a code that extracts some parameters from some data in the following way:
LasingThreshold=0.1;
% %---------Convert xlsx to mat - only need to use once----------------------
files = dir('LIV Data*.xlsx');
for k = 1:numel(files)
[num,text,raw] = xlsread(files(k).name);
[dummy, myName] = fileparts(files(k).name);
save(myName)
end
%--------------------------------------------------------------------------
D = uigetdir
cd(D);
S = dir(fullfile(D,'LIV Data T= *.mat')); % Makes a structure with all files
N = numel(S); % Counts number of files
CM = cool(N); % set colormap
fig1 = figure;
Pdissarray = zeros(N,52)
for ii = 1:N, j = 1:52;
T = load(fullfile(D,S(ii).name)); % Load each file as a structure
I = T.num(3:end,1); % Extract each parameter from full file and ignore first two data points
V = T.num(3:end,2);
P = T.num(3:end,3);
Pelec = V.*I;
Pdiss = Pelec-P;
Pdissarray(ii,j) = Pdissarray(ii,j)+Pdiss(ii)
DataPoints = length(I);
k = find(P>LasingThreshold,1);
fit = polyfit(I(k:round(DataPoints/2)),P(k:round(DataPoints/2)),1); %Need value of P to be greater than the laser threshold - this should be done better eventually
fitplot = polyval(fit, I);
slope(ii) = fit(1);
iTh(ii) = -fit(2)/fit(1);
subplot(1,2,1);
plot(I,P,'.','markers',5,'color',CM(ii,:))
plot(I,fitplot,'-.','color',CM(ii,:))
ylim([0 4]);
xlabel('Current (mA)')
ylabel('Power (mW)')
hold on
subplot(1,2,2);
plot(I,V,'.-','markers',4,'color',CM(ii,:))
xlabel('Current (mA)')
ylabel('Voltage (V)')
hold on
end
I'm having a problem with Pdissarray - I want it to store all of the values of Pdiss for each file. I'm currently trying to put this into a single array but that doesn't have to be the way I do it, I just need to retain the values in some form. Currently it is just taking the final value of Pdiss for each file and repeating it over the array. I have included two files with the code.

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 11월 20일
The simplest way is to use the indexing to assign the result from each loop to your array.
However, I think you need to create two for loops here:
for ii = 1:N
for j = 1:52
...
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by