Matrix from for loop displaying vectors of different length
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
KSSV
le 10 Juil 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!