필터 지우기
필터 지우기

Matrix from for loop displaying vectors of different length

조회 수: 1 (최근 30일)
Arthur Batte
Arthur Batte 2020년 7월 9일
댓글: KSSV 2020년 7월 11일
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

채택된 답변

KSSV
KSSV 2020년 7월 10일
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
Arthur Batte
Arthur Batte 2020년 7월 10일
thank you very much
KSSV
KSSV 2020년 7월 11일
Thanks is accepting/ voting te answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by