What's the best way to shorten this?
Mostrar comentarios más antiguos
This is a very lengthy code for something repetitive, I want to know whether there is a simpler, more efficient way of doing this.
The purpose of the code is to load text files and trim them according to the same criteria and check which one is the shortest and use the shortest length in calculations that happen later in the code.
T1 = dlmread("Trial 1");
T2 = dlmread("Trial 2");
T3 = dlmread("Trial 3");
T4 = dlmread("Trial 4");
T5 = dlmread("Trial 5");
T6 = dlmread("Trial 6");
T7 = dlmread("Trial 7");
T8 = dlmread("Trial 8");
T9 = dlmread("Trial 9");
T10 = dlmread("Trial 10");
%removal of unwanted elements from the raw data array
T1(T1(:,11)<=0 ,:) = [];
T1(T1(:,3)==25 ,:) = [];
T1(T1(:,3)<500 ,:) = [];
T1(T1(:,5)==0 ,:)= [];
T1(find(T1(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T2(T2(:,3)==25 ,:) = [];
T2(T2(:,3)<500 ,:) = [];
T2(T2(:,5)==0 ,:)= [];
T2(find(T2(:,12)>780,1):end, :) = [];
T2(T2(:,11)<=0 ,:) = [];
T3(T3(:,3)==25 ,:) = [];
T3(T3(:,3)<500 ,:) = [];
T3(T3(:,5)==0 ,:)= [];
T3(find(T3(:,12)>780,1):end, :) = [];
T4(T4(:,11)<=0 ,:) = [];
T4(T4(:,3)==25 ,:) = [];
T4(T4(:,3)<500 ,:) = [];
T4(T4(:,5)==0 ,:)= [];
T4(find(T4(:,12)>780,1):end, :) = [];
T5(T5(:,11)<=0 ,:) = [];
T5(T5(:,3)==25 ,:) = [];
T5(T5(:,3)<500 ,:) = [];
T5(T5(:,5)==0 ,:)= [];
T5(find(T5(:,12)>780,1):end, :) = [];
T6(T6(:,11)<=0 ,:) = [];
T6(T6(:,3)==25 ,:) = [];
T6(T6(:,3)<500 ,:) = [];
T6(T6(:,5)==0 ,:)= [];
T6(find(T6(:,12)>780,1):end, :) = [];
T7(T7(:,11)<=0 ,:) = [];
T7(T7(:,3)==25 ,:) = [];
T7(T7(:,3)<500 ,:) = [];
T7(T7(:,5)==0 ,:)= [];
T7(find(T5(:,12)>780,1):end, :) = [];
T8(T8(:,11)<=0 ,:) = [];
T8(T8(:,3)==25 ,:) = [];
T8(T8(:,3)<500 ,:) = [];
T8(T8(:,5)==0 ,:)= [];
T8(find(T8(:,12)>780,1):end, :) = [];
T9(T9(:,11)<=0 ,:) = [];
T9(T9(:,3)==25 ,:) = [];
T9(T9(:,3)<500 ,:) = [];
T9(T9(:,5)==0 ,:)= [];
T9(find(T9(:,12)>780,1):end, :) = [];
T10(T10(:,11)<=0 ,:) = [];
T10(T10(:,3)==25 ,:) = [];
T10(T10(:,3)<500 ,:) = [];
T10(T10(:,5)==0 ,:)= [];
T10(find(T10(:,12)>780,1):end, :) = [];
L1=length(T1);
L2=length(T2);
L3=length(T3);
L4=length(T4);
L5=length(T5);
L6=length(T6);
L7=length(T7);
L8=length(T8);
L9=length(T9);
L10=length(T10);
Lmin = min([L1(:);L2(:);L3(:);L4(:);L5(:);L6(:);L7(:);L8(:);L9(:);L10(:)]);
3 comentarios
John D'Errico
el 9 de Mayo de 2019
Can I suggest this is why creating those numbered variables is such a terrible idea? Instead, learn to use arrays. A cell array, for example, would make it trivial to write the entire mess in one trivial to write (and short) for loop.
Stephen23
el 13 de Mayo de 2019
Using numbered variables is a sign that you are doing something wrong.
Copy-and-pasting code is a sign that you are doing something wrong.
Remember that computers are only really good at one thing: repeatedly doing simple tasks. So when you find yourself copy-and-pasting the same code over and over again, then you are just doing the computer's job for it. Instead get the computer to do it for you: use a loop.
Hans123
el 13 de Mayo de 2019
Respuesta aceptada
Más respuestas (1)
Kevin Phung
el 9 de Mayo de 2019
1 voto
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!