How to use function table (or timetable) inside a loop to generate a final table that contains a large number of variables (columns) with names

조회 수: 9 (최근 30일)
Wondering if anyone could suggest how to use the function table (or timetable) inside a loop to generate a final table that contains a large number of variables (columns) with names?
%This is my wrong attempt...
T = array2table(DATA);
%DATA is an array with 1st column (sorted dates), following 300 columns (data, double), and over 100.000 rows.
for kk=1:size(DATA,2)
name={num2str(file_name_list{kk})}; % Generate individual names to add to next step
T =[T; array2table(DATA,'VariableNames', {name})]; % Trying to add new names to columns...
end
%... to get something like
Time VarName1 VarName2 ... VarName300
'20-Sep-2001 00:00:00' 10 2.5 ... NaN
'20-Sep-2001 01:00:00' NaN 0.34 ... 12
'20-Sep-2001 02:00:00' 0.1 NaN ... 0.4
... ... ... ... ...
Thanks!

채택된 답변

Peter Perkins
Peter Perkins 2017년 3월 8일
I'm not sure what is actually in the array DATA. I may have missed that in your description. It sounds like it might be a numeric array with datenums in column 1 and numbers in columns 2:300 (or 2:301?). And then file_list is a 1x300 (1x301?) cell array of char vectors like 'AGR_OCEAN_001'.
So maybe
tt = array2timetable(DATA(2:300), ...
'RowTimes',datetime(Data,'ConvertFrom','datenum'), ...
'VariableNames',file_list)
No loop needed. But I think we're gonna need to see a real, small example of what you have.
  댓글 수: 3
Robert
Robert 2017년 3월 8일
That works great! Thank you to all of you for make time to help people like me to learn and advance in Matlab world. Very appreciated!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 3월 7일
Almost!
T =[T, array2table(DATA,'VariableNames', {name})]; % Trying to add new names to columns...
This changes the ";" to ","
For example,
T = array2table(randi(6,5,2))
T = [T,array2table(randi(10,5,7), 'VariableNames', {'A1','A2','A3','A4','A5','A6','A7'})]
  댓글 수: 5
Walter Roberson
Walter Roberson 2017년 3월 8일
My guess:
T = array2table(DATA(:,1));
%This now create a Table with one column, which has the dates
for kk = 2 : size(DATA,2)
name_cell = file_list(kk);
% Generate individual names to add to next step
T = [T, array2table(DATA(:,kk),'VariableNames', name_cell)];
end
However, if you were going to do that, then just
T = array2table(DATA, 'VariableNames', file_list);
with no loop.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by