Matrix from for loop displaying vectors of different length

1 visualización (últimos 30 días)
Arthur Batte
Arthur Batte el 9 de Jul. de 2020
Comentada: KSSV el 11 de Jul. de 2020
Hello, could someone advice. I have wriiten some code (readout.m) that reads a file containing names of 3 data files (filenr.lis). These 3 data files are stored in a directory named wavf. These 3 data files are not of the same length. My code outputs all the 3 datas as vectors of different lengths and stores the results in a variable named output. Unfortunately i have failed to save the 3 results in a matrix whereby the length of the matrix is scaled by the smallest length. I mean trim the datasets such that they have the same lengths and then output the result as a matrix outside the loop. I have attached the datasets in a zip. thx

Respuesta aceptada

KSSV
KSSV el 10 de Jul. de 2020
You need to interpolate them to the same size using interp1.
clc
clear
close all
[index, sname] = textscan('filenr.lis','%d %s');
N = length(index) ;
iwant = cell(N,1) ;
for k = 1:N
wavfilepath = '\\GSE\\';
sfiles =char(sname(k,:));
s = [wavfilepath sfiles];
fid = fopen(s);
[data] = readfile(fid);
iwant{k} = data{1}; % load data of the file
end
% do interpolatio to make them to same size
L = cellfun(@length,iwant) ; % length of each array
Lmax = max(L) ;
data = zeros(L,N) ;
for i = 1:N
x = 1:length(iwant{i}) ;
y = iwant{i} ;
xi = linspace(1,length(iwant{i}),Lmax) ;
data(:,i) = interp1(x',y,xi') ;
end
  2 comentarios
KSSV
KSSV el 11 de Jul. de 2020
Thanks is accepting/ voting te answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by