How to minimize the length of my script using loops

2 vues (au cours des 30 derniers jours)
Navaneeth Tejasvi Mysore Nagendra
Commenté : Chandra le 1 Juin 2022
I have a scrip specifically to plot many graphs for my data, I have 15 source files from which I am extracting data to plot the necessary plots. The issue is the scrip is too long and I would like to make it small by using loops, but I dont know much about how to start with it. So I ask anykind of help to direct me in right direction
my program,
clc;clear;close all;
% known variables
frequency = [100,1e3,10e3,100e3]./1e3;
%to calculate dielectric constant
eps_0 = 8.85418782e-12;
di1 = 0.18e-2;
r1 = di1/2;
A1 = pi*r1^2;
d = 300e-9;
c_01 = eps_0*A1/d;
di2 = 0.18e-2;
r2 = di2/2;
A2 = pi*r2^2;
c_02 = eps_0*A2/d;
di3 = 0.18e-2;
r3 = di3/2;
A3 = pi*r3^2;
c_03 = eps_0*A3/d;
%To import my data
% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 4);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["SN", "PrimaryCF", "SecondaryD", "Var4"];
opts.SelectedVariableNames = ["SN", "PrimaryCF", "SecondaryD"];
opts.VariableTypes = ["double", "double", "double", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "Var4", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "Var4", "EmptyFieldRule", "auto");
CD1 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\C_D_LS1", opts);
CD2 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\C_D_LS2", opts);
CD3 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\C_D_LS3", opts);
CD4 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\C_D_LS4", opts);
CD5 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\C_D_LS5_T2", opts);
ZESR1 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\Z_ESR_LS1", opts);
ZESR2 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\Z_ESR_LS2", opts);
ZESR3 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\Z_ESR_LS3", opts);
ZESR4 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\Z_ESR_LS4", opts);
ZESR5 = readtable("D:\Master thesis\3doxides\lcr meter\data\Al2O3_HfO2_ZrO2_TiO2Nb\Z_ESR_LS5", opts);
%tables to arrays
CD1 = table2array(CD1);
CD2 = table2array(CD2);
CD3 = table2array(CD3);
CD4 = table2array(CD4);
CD5 = table2array(CD5);
ZESR1 = table2array(ZESR1);
ZESR2 = table2array(ZESR2);
ZESR3 = table2array(ZESR3);
ZESR4 = table2array(ZESR4);
ZESR5 = table2array(ZESR5);
%creating arrays
%capacitance
C1 = [CD1(1,2),CD1(15,2),CD1(25,2),CD1(35,2)]./1e-9;
C2 = [CD2(5,2),CD2(15,2),CD2(25,2),CD2(35,2)]./1e-9;
C3 = [CD3(5,2),CD3(15,2),CD3(25,2),CD3(35,2)]./1e-9;
C4 = [CD4(5,2),CD4(15,2),CD4(25,2),CD4(35,2)]./1e-9;
C5 = [CD5(9,2),CD5(15,2),CD5(25,2),CD5(35,2)]./1e-9;
%impedance
Z1 = [ZESR1(1,2),ZESR1(15,2),ZESR1(25,2),ZESR1(35,2)];
Z2 = [ZESR2(5,2),ZESR2(15,2),ZESR2(25,2),ZESR2(35,2)];
Z3 = [ZESR3(5,2),ZESR3(15,2),ZESR3(25,2),ZESR3(35,2)];
Z4 = [ZESR4(5,2),ZESR4(15,2),ZESR4(25,2),ZESR4(35,2)];
Z5 = [ZESR5(9,2),ZESR5(15,2),ZESR5(25,2),ZESR5(35,2)];
%dissipation
D1 = [CD1(1,3),CD1(15,3),CD1(25,3),CD1(35,3)];
D2 = [CD2(5,3),CD2(15,3),CD2(25,3),CD2(35,3)];
D3 = [CD3(5,3),CD3(15,3),CD3(25,3),CD3(35,3)];
D4 = [CD4(5,3),CD4(15,3),CD4(25,3),CD4(35,3)];
D5 = [CD5(9,3),CD5(15,3),CD5(25,3),CD5(35,3)];
% to export variables as .txt file
%transposed datas
CT1 = C1';
CT2 = C2';
CT3 = C3';
CT4 = C4';
CT5 = C5';
DT1 = D1';
DT2 = D2';
DT3 = D3';
DT4 = D4';
DT5 = D5';
ZT1 = Z1';
ZT2 = Z2';
ZT3 = Z3';
ZT4 = Z4';
ZT5 = Z5';
TC = table(CT1,CT2,CT3,CT4,CT5);
writetable(TC,'capcitance.txt')
TZ = table(ZT1,ZT2,ZT3,ZT4,ZT5);
writetable(TZ,'impedance.txt')
TD = table(DT1,DT2,DT3,DT4,DT5);
writetable(TD,'loss.txt')
figure(1)
clear opts;
lg1 = loglog(frequency,C1,'-or');
lg1.LineWidth = 2;
hold on;
lg2 = loglog(frequency,C2,'-og');
lg2.LineWidth = 2;
hold on;
lg3 = loglog(frequency,C3,'-ob');
lg3.LineWidth = 2;
hold on;
lg4 = loglog(frequency,C4,'-oy');
lg4.LineWidth = 2;
hold on;
lg5 = loglog(frequency,C5,'-oc');
lg5.LineWidth = 2;
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('Capacitance (C) in nF)');
lgd1=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd1.NumColumns=2
lgd1.FontSize = 12
grid on;
print(gcf,'Capacitance.jpg','-dpng','-r300')
figure(2)
clear opts;
lg1 = loglog(frequency,D1,'-or');
lg1.LineWidth = 2;
hold on;
lg2 = loglog(frequency,D2,'-og');
lg2.LineWidth = 2;
hold on;
lg3 = loglog(frequency,D3,'-ob');
lg3.LineWidth = 2;
hold on;
lg4 = loglog(frequency,D4,'-oy');
lg4.LineWidth = 2;
hold on;
lg5 = loglog(frequency,D5,'-oc');
lg5.LineWidth = 2;
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('tan(\delta)');
lgd2=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd2.NumColumns=2
lgd2.FontSize = 12
grid on;
print(gcf,'Dissipation.jpg','-dpng','-r300')
figure(3)
clear opts;
lg1 = loglog(frequency,Z1,'-or');
lg1.LineWidth = 2;
hold on;
lg2 = loglog(frequency,Z2,'-og');
lg2.LineWidth = 2;
hold on;
lg3 = loglog(frequency,Z3,'-ob');
lg3.LineWidth = 2;
hold on;
lg4 = loglog(frequency,Z4,'-oy');
lg4.LineWidth = 2;
hold on;
lg5 = loglog(frequency,Z5,'-oc');
lg5.LineWidth = 2;
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('Impedance in \Omega');
lgd1=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd1.NumColumns=2
lgd1.FontSize = 12
grid on;
print(gcf,'impedance.jpg','-dpng','-r300')

Réponses (1)

Chandra
Chandra le 24 Mai 2022
Hi,
clc;clear;close all;
% known variables
frequency = [100,1e3,10e3,100e3]./1e3;
%to calculate dielectric constant
eps_0 = 8.85418782e-12;
di1 = 0.18e-2;
r1 = di1/2;
A1 = pi*r1^2;
d = 300e-9;
c_01 = eps_0*A1/d;
di2 = 0.18e-2;
r2 = di2/2;
A2 = pi*r2^2;
c_02 = eps_0*A2/d;
di3 = 0.18e-2;
r3 = di3/2;
A3 = pi*r3^2;
c_03 = eps_0*A3/d;
%To import my data
% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 4);
% Specify range and delimiter
opts.DataLines = [2, Inf];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["SN", "PrimaryCF", "SecondaryD", "Var4"];
opts.SelectedVariableNames = ["SN", "PrimaryCF", "SecondaryD"];
opts.VariableTypes = ["double", "double", "double", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "Var4", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "Var4", "EmptyFieldRule", "auto");
CD = cell(1,5);
ZESR = cell(1,5);
for i=1:5
CD{i} = readtable(strcat("C_D_LS",num2str(i)),opts);
ZESR{i} = readtable(strcat("Z_ESR_LS",num2str(i)), opts);
end
AA = [1 15 25 35;5 15 25 35;5 15 25 35;5 15 25 35;9 15 25 35 ];
BB = [2*ones(1,5);5*ones(1,5);8*ones(1,5);11*ones(1,5);14*ones(1,5)];
CC = AA;
DD = BB+1;
EE = AA;
FF = BB;
CD1 = cell(1,5);
C = cell(1,5);
D = cell(1,5);
Z = cell(1,5);
ZESR1 = cell(1,5);
for i = 1:5
CD1{i} = table2array(CD{i});
CD_1(:,(i*3-2:i*3)) = cell2mat(CD1(i));
C(i) = {[CD_1(AA(i,1),BB(i,1)),CD_1(AA(i,2),BB(i,2)),CD_1(AA(i,3),BB(i,3)),CD_1(AA(i,4),BB(i,4))]./1e-9};
D(i) = {[CD_1(CC(i,1),DD(i,1)),CD_1(CC(i,2),DD(i,2)),CD_1(CC(i,3),DD(i,3)),CD_1(CC(i,4),DD(i,4))]./1e-9};
ZESR1{i} = table2array(CD{i});
ZESR_1(:,(i*3-2:i*3)) = cell2mat(ZESR1(i));
Z(i) = {[ZESR_1(EE(i,1),FF(i,1)),ZESR_1(EE(i,2),FF(i,2)),ZESR_1(EE(i,3),FF(i,3)),ZESR_1(EE(i,4),FF(i,4))]./1e-9};
end
B =[];
for i = 1:5
B = [B;cell2mat(C(i))];
end
for i = 1:5
B = [B;cell2mat(D(i))];
end
for i = 1:5
B = [B;cell2mat(Z(i))];
end
TC = table(B(1,:)',B(2,:)',B(3,:)',B(4,:)',B(5,:)');
writetable(TC,'capcitance.txt')
TZ = table(B(6,:)',B(7,:)',B(8,:)',B(9,:)',B(10,:)');
writetable(TZ,'impedance.txt')
TD = table(B(11,:)',B(12,:)',B(13,:)',B(14,:)',B(15,:)');
writetable(TD,'loss.txt')
figure(1)
clear opts;
cc = {'-or' '-og' '-ob' '-oy' '-oc'};
for i = 1:5
A = loglog(frequency,B(i,:),cell2mat(cc(i)));
A.LineWidth = 2;
clear A;
hold on;
end
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('Capacitance (C) in nF)');
lgd1=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd1.NumColumns=2
lgd1.FontSize = 12
grid on;
print(gcf,'Capacitance.jpg','-dpng','-r300')
figure(2);
for i = 1:5
A = loglog(frequency,B(i+5,:),cell2mat(cc(i)));
A.LineWidth = 2;
hold on;
end
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('tan(\delta)');
lgd2=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd2.NumColumns=2;
lgd2.FontSize = 12;
grid on;
print(gcf,'Dissipation.jpg','-dpng','-r300')
figure(3)
for i = 1:5
A = loglog(frequency,B(i+10,:),cell2mat(cc(i)));
A.LineWidth = 2;
hold on;
end
set(gca, 'LineWidth', 1.5)
set(gca,'fontweight','bold','fontsize',16);
xlabel('Frequency (KHz)');
ylabel('Impedance in \Omega');
lgd1=legend('R-S1','R-S2','R-S3','R-S4','R-S5');
lgd1.NumColumns=2
lgd1.FontSize = 12
grid on;
print(gcf,'impedance.jpg','-dpng','-r300')
change the path files accordingly before executing the code
In this code "for" loop is used for input and ploting the graphs and some functions like "cell2mat" and "cell" are used to help "for" loop
to reduce further, write repeated lines in a function and call the function.
  3 commentaires
Navaneeth Tejasvi Mysore Nagendra
Hi, I was trying this code on my data and I am constantly getting this error,
Unable to perform assignment because the size of the left side is 40-by-3 and the size of the right side is 42-by-3.
Error in short_script_plots (line 59)
CD_1(:,(i*3-2:i*3)) = cell2mat(CD1(i));
any clue why this is? I tried to resolve myself but not able to. Thanks in advance
Chandra
Chandra le 1 Juin 2022
Hi,
The length varied as previous table data may have 40 as row length and current CD1 have 42 row length try to adjust the number of rows length of table to equal number or try zero padding.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by