## What's the best way to shorten this?

Hans123

### Hans123 (view profile)

님이 질문을 제출함. 9 May 2019

님이 댓글을 추가함. 13 May 2019

님이 답변을 채택함.
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.
%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(:)]);

John D'Errico

### John D'Errico (view profile)

9 May 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.
Stephen Cobeldick

### Stephen Cobeldick (view profile)

13 May 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

### Hans123 (view profile)

13 May 2019
thank you for your inputs, I really appreciate it!

로그인 to comment.

## 답변 수: 2

님의 답변 9 May 2019

님이 편집함. 13 May 2019
채택된 답변

This should get you started but it's not tested and there will likely be some kinks to work out. I'd be glad to help more if needed.
trials = ["Trial 1", "Trial 2", "Trial 3", "Trial 4", "Trial 5", ...
"Trial 6", "Trial 7", "Trial 8", "Trial 9", "Trial 10" ];
nTrials = numel(trials);
T = cell(size(trials));
for i = 1:nTrials
%removal of unwanted elements from the raw data array
T{i}(T{i}(:,11)<=0 ,:) = [];
T{i}(T{i}(:,3)<500 ,:) = [];
T{i}(T{i}(:,5)==0 ,:)= [];
T{i}(find(T{i}(:,12)>780,1):end, :) = [];
end
L = cellfun(@length, T,'UniformOutput', false);
Lmin = min([L{:}]);

Hans123

### Hans123 (view profile)

13 May 2019
this works perfectly, I really appreciate the help. I am relatively new to MATLAB and cell functions as well. I didn't quite understand how this line works
L = cellfun(@length, T,'UniformOutput', false);
Any help would be appreciated.
Cheers

13 May 2019
The first input to cellfun() is a function handle. The second input is a cell array. The cellfun() function applies the function in input #1 to each element of the cell array in input #2. For example, the cell array T (defined below) has 4 elements. The length of the four elements are [3, 4, 5, 2].
T = { [1 2 3], [1 2 3 4], [1 2 3 4 5], [1 2] };
The 'UniformOutput' flag specifies that the output should be stored in a cell array.

로그인 to comment.

Kevin Phung

님의 답변 9 May 2019

로그인 to comment.

Translated by