How to store multiple vectors in an array?

23 次查看(过去 30 天)
Hey there,
i have a problem to store my vectors (force measurement signals), that have all different length, in an array using a for loop.
I am using Matlab App Designer and y/x are properties. The Error i get is: Unable to perform assignment because the size of the left side is 1-by-5319 and the size of the right side is 8545-by-1. So what i understand is that the first iteration performs succesfully, but what i dont understand why the stored data is 1x5319 (column), because the data im filtering is 5319x1(row) (this is for example the first data im reading in). I know there is a lot of information about storing data, but im kinda new to matlab and im getting a bit confused here. So thanks for any help!
for i = 1:length(filename)
filepath=fullfile(pathname, filename);
fid = fopen (filepath{i});
%[file,path]=uigetfile('*.csv');
A = textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s','Delimiter',';');
%Umwandeln von Cell --> String
B=string([A{:}]);
%',' durch '.' ersetzen
B=replace(B,',', '.'); % ',' durch '.' ersetzen
% Aufteilen in Header und Messdaten
Daten(i).Header=B(1:30,:);
Daten(i).Messdaten=str2double(B(31:end,7));
end
for i = 1:length(filename)
%Uhrzeit / time
u= char(Daten(i).Header(12,1));
u1 = extractBetween(u,13,20 );
%Datum / date
d = char(Daten(i).Header(11,1));
d1 = extractBetween(d,11,20);
%Bein Seite / leg
be = char(Daten(i).Header(21,1));
if strlength(be) == 22
be1 = extractBetween(be,10,14);
else
be1 = extractBetween(be,10,15);
end
% Bewegung /
b = char(Daten(i).Header(23,1));
if strlength(b) == 32
b1 = extractBetween(b,13,21);
else
b1 = extractBetween(b,13,19);
end
% Kraft Tiefpass Filter / lowpass filter
app.x = Daten(i).Messdaten;
windowsize = 5;
c = (1/windowsize)*ones(1,windowsize);
a = 1;
app.y(i,:) = filter(c,a,app.x); % This line is causing the Error
M =max(app.y);
% Einlesen der Daten in Tabelle
data(i,:) = [d1 u1 b1 be1 M];
end

采纳的回答

Walter Roberson
Walter Roberson 2021-2-2
app.y(i,:) = filter(c,a,app.x);
MATLAB will notice that the output size is 1 x something. It will calculate the right hand value. It will examine the size of the right. If it is empty in any dimension and this is the first iteration it will make and as long as it has only one dimension that y into a 1x0 array. If there are no empty dimensions and there are more than one dimensions larger than 1, then it will error. Otherwise there must be no empty dimensions and at most one dimension larger than 1. Knowing that the needed output is 1 x something then if this is the first iteration, it will reshape the right to be a row vector no matter which original dimension was the one that is non-singular, and it will create y as one row with the appropriate number of elements.
If the right side is not empty and has one dimension that is not singular, then matlab checks that the length of that dimension is the same as the number of columns in y and errors if there is a difference. That is the error you are facing.
If the right side is scalar and this is not the first iteration then matlab looks up the number of columns in the matrix and copies the scalar value into every column of that row.
In MATLAB numeric matrices cannot have different numbers of columns for each row. If you need different numbers of columns then you cannot use numeric matrices should consider cell arrays.
In some situations, it is acceptable to store rows of different lengths by padding the unused space with zeros or a constant such as -9999 or nan. The code to do that is an easy change if you are padding with 0:
app.y(i,1:length(x)) = x;
Instead of
app.y(i,:) = x;
If you are padding with anything other than zero then it takes a few more lines.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by