How to optimise the code by using condition/loop ?

1 visualización (últimos 30 días)
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa el 3 de Nov. de 2020
Comentada: Jeevan Kumar Bodaballa el 3 de Nov. de 2020
I have 20 mat files contains two varibles each(let say A,B).
I am calculating linear model between two varibles with fitlm function and I got output like r_square and all
and then I am writing them into TEXT file. I have long code for that
Instead writing each varibles, I want to optimise the code by writing loop/condition
Here is the code
clear;close all
% loading files
load f1_pH_con.mat;load f2_pH_con.mat;load f3_pH_con.mat
load f4_pH_con.mat;load f5_pH_con.mat;load f6_pH_con.mat
load f7_pH_con.mat;load f8_pH_con.mat;load f9_pH_con.mat
load f10_pH_con.mat;load f11_pH_con.mat;load f12_pH_con.mat
load f13_pH_con.mat;load f14_pH_con.mat;load f15_pH_con.mat
load f16_pH_con.mat;load f17_pH_con.mat;
load f18_pH_con.mat;load f19_pH_con.mat;load f20_pH_con.mat
% removing errors(making values NaN if any lesss than zero)
f1_pH(f1_pH<=0)=NaN;f2_pH(f2_pH<=0)=NaN;f3_pH(f3_pH<=0)=NaN;
f4_pH(f4_pH<=0)=NaN;f5_pH(f5_pH<=0)=NaN;f6_pH(f6_pH<=0)=NaN;
f7_pH(f7_pH<=0)=NaN;f8_pH(f8_pH<=0)=NaN;f9_pH(f9_pH<=0)=NaN;
f10_pH(f10_pH<=0)=NaN;f11_pH(f11_pH<=0)=NaN;f12_pH(f12_pH<=0)=NaN;
f13_pH(f13_pH<=0)=NaN;f14_pH(f14_pH<=0)=NaN;f15_pH(f15_pH<=0)=NaN;
f16_pH(f16_pH<=0)=NaN;f17_pH(f17_pH<=0)=NaN;f18_pH(f18_pH<=0)=NaN;
f19_pH(f19_pH<=0)=NaN;f20_pH(f20_pH<=0)=NaN;
f1_con(f1_con<=0)=NaN;f2_con(f2_con<=0)=NaN;f3_con(f3_con<=0)=NaN;
f4_con(f4_con<=0)=NaN;f5_con(f5_con<=0)=NaN;f6_con(f6_con<=0)=NaN;
f7_con(f7_con<=0)=NaN;f8_con(f8_con<=0)=NaN;f9_con(f9_con<=0)=NaN;
f10_con(f10_con<=0)=NaN;f11_con(f11_con<=0)=NaN;f12_con(f12_con<=0)=NaN;
f13_con(f13_con<=0)=NaN;f14_con(f14_con<=0)=NaN;f15_con(f15_con<=0)=NaN;
f16_con(f16_con<=0)=NaN;f17_con(f17_con<=0)=NaN;f18_con(f18_con<=0)=NaN;
f19_con(f19_con<=0)=NaN;f20_con(f20_con<=0)=NaN;
% calculating linear model
mdl1 = fitlm(f1_pH,f1_con);mdl2 = fitlm(f2_pH,f2_con)
mdl3 = fitlm(f3_pH,f3_con);mdl4 = fitlm(f4_pH,f4_con)
mdl5 = fitlm(f5_pH,f5_con);mdl6 = fitlm(f6_pH,f6_con)
mdl7 = fitlm(f7_pH,f7_con);mdl8 = fitlm(f8_pH,f8_con)
mdl9 = fitlm(f9_pH,f9_con);mdl10 = fitlm(f10_pH,f10_con)
mdl11 = fitlm(f11_pH,f11_con);mdl12 = fitlm(f12_pH,f12_con)
mdl13 = fitlm(f13_pH,f13_con);mdl14 = fitlm(f14_pH,f14_con)
mdl15 = fitlm(f15_pH,f15_con);mdl16 = fitlm(f16_pH,f16_con)
mdl17 = fitlm(f17_pH,f17_con);mdl18 = fitlm(f18_pH,f18_con)
mdl19 = fitlm(f19_pH,f19_con);mdl20 = fitlm(f20_pH,f20_con)
% converting to char for writing into txt file
f1 = evalc('mdl1');f2 = evalc('mdl2');f3 = evalc('mdl3');f4 = evalc('mdl4');
% writing into txt file
fid = fopen(fullfile('regressoion.txt'),'wt');
fprintf(fid,'%s',T);
fclose(fid);

Respuesta aceptada

Rik
Rik el 3 de Nov. de 2020
for n=1:20
S=load(sprintf('f%d_pH_con.mat',n));
f_pH{n}=S.(sprintf('f%d_pH',n));%undo the numbered variable mess
f_con{n}=S.(sprintf('f%d_con',n));%undo the numbered variable mess
f_pH{n}(f_pH{n}<=0)=NaN;
f_con{n}(f_con{n}<=0)=NaN;
mdl{n}=fitlm(f_pH{n},f_con{n});
end
Now you have everything neatly indexed, so you can redesing the way you convert your models to char arrays (not string objects, as your comment claims).
  5 comentarios
Rik
Rik el 3 de Nov. de 2020
You should not do that. You see the difference between my code and yours. With my code it is trivial to do the same for 200 files, with your code that is a Herculean task.
What exactly about these models do you want to store in the text file? That is the only part my code doesn't do and yours does.
Jeevan Kumar Bodaballa
Jeevan Kumar Bodaballa el 3 de Nov. de 2020
I understand. Thank you :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by