Search code examples
matlabstrcat

MATLAB : Trim the zeros in the arrays and concatenate variable dimension columns to from a matrix


The output of my code is as follows

load(1).value(1:end) = [0.0100 0.5030 0.7590 1]

load(2).value(1:end) = [0 0 0 0 1]

load(3).value(1:end) = [0 0 0 0 0 0.2000 0.4000]

But I want

load(1).value(1:end) = [0.0100 0.5030 0.7590 1]

load(2).value(1:end) = [1]

load(3).value(1:end) = [.2000 0.4000]

How can I trim the "leading zeroes, that occur from load(2)" away and later concatenate these array's vertically together in xlsx or csv format?

the csv file should contain:'0.0100 0.5030 0.7590 1' in 1st column, '1' in second column and '.2 .4' in third column


Solution

  • If you save those values in a intermediate array you could select the index equal to zero and delete them.

    aux=[0.0100 0.5030 0.7590 1]
    aux(aux==0)=[];
    load(1).value(1:end) = aux;
    
    aux=[0 0 0 0 1]
    aux(aux==0)=[];
    load(2).value(1:end) = aux;
    
    aux=[0 0 0 0 0 0.2000 0.4000]
    aux(aux==0)=[];
    load(3).value(1:end) = aux;
    

    For the concatenation, check vertcat(), for vertical concatenation.