Filter löschen
Filter löschen

Matrix from for loop displaying vectors of different length

1 Ansicht (letzte 30 Tage)
Arthur Batte
Arthur Batte am 9 Jul. 2020
Kommentiert: KSSV am 11 Jul. 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

Akzeptierte Antwort

KSSV
KSSV am 10 Jul. 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by