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

4 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 20 Nov 2018
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

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by