Asked by Hans123
on 9 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.

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(:)]);

Answer by Adam Danz
on 9 May 2019

Edited by Adam Danz
on 13 May 2019

Accepted Answer

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

T{i} = dlmread(trials(i));

%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
on 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

Adam Danz
on 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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## John D'Errico (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/461215-what-s-the-best-way-to-shorten-this#comment_703648

## Stephen Cobeldick (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/461215-what-s-the-best-way-to-shorten-this#comment_704616

## Hans123 (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/461215-what-s-the-best-way-to-shorten-this#comment_704767

Sign in to comment.