Slicing tables into n columns and loops

29 次查看(过去 30 天)
Alex
Alex 2013-11-5
回答: Simon 2013-11-5
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!

回答(1 个)

Simon
Simon 2013-11-5
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

类别

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

产品

Community Treasure Hunt

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

Start Hunting!

Translated by