Borrar filtros
Borrar filtros

Slicing tables into n columns and loops

14 visualizaciones (últimos 30 días)
Alex
Alex el 5 de Nov. de 2013
Respondida: Simon el 5 de Nov. de 2013
Hello everybody,
I have a table with 7 columns and many rows. The first column contains times and the remaining 6 data sets. I need to slice this table into columns. I cannot "make assumptions about how many sets of measurements are in the data file" i.e. I can't do this:
_t = raw(:,1);
d1 = raw(:,2);
d2 = raw(:,3);_ … where "raw" is the name of the table and d1, d2… is the name of the new row vector.
it has to work regardless of how many data sets there are. How can I do that?
Also, I have to do a linear fit or restricted linear fit on those vectors (x axis is always t and y axis is d1, d2…), depending on whether the vector starts with 0 or not. I came up with
_if d1(1,:) == 0
opts1 = fitoptions('poly1', 'Lower', [-Inf 0], 'Upper', [Inf 0]);
[f1, gof] = fit(t, d1,'poly1', opts);
else
[f1, gof] = fit(t, d1,'poly1');
end_
which works if I slice the columns one by one and change the d1, d2… but I assume that I need to make that into a loop? Something like "for ii = something"?
I hope you can understand my question, it's hard to explain…
I hope someone out there can help me! Thanks in advance!

Respuestas (1)

Simon
Simon el 5 de Nov. de 2013
Hi!
Your data in an array is something like
data = row(:, 2:end);
You do not have to write the columns to separate variables. The fit in a loop is like
% prepare fit result array
fitresult = zeros(size(data));
% loop over all columns
for n = 1:size(data, 2)
% check first element in column
if data(1, n) == 0
opts1 = fitoptions('poly1', 'Lower', [-Inf 0], 'Upper', [Inf 0]);
[fitresult(:, n), gof] = fit(t, data(:, n),'poly1', opts);
else
[fitresult(:, n), gof] = fit(t, data(:, n),'poly1');
end
end

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