For loop getting array name

4 次查看(过去 30 天)
James Browne
James Browne 2021-6-21
编辑: Stephen23 2021-6-22
Hi,
I am reading in a series of tables.
I then want to run a for loop performing operations on each of these tables. To do this I want to use the 'I'th name in my array.
My code looks like this:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
%% Import the data
Table_A = readtable("Table_A", opts);
Table_B = readtable("Table_B", opts);
Table_C = readtable("Table_C", opts);
%% Defining the datasets to perform operations on
datalist = ["Table_A ","Table_B","Table_C"];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = [1,2,3];
ID=datalist(i); % I want this to be the table "Table_A" so that I can peform operations in this for loop
Table_ID = datalist(i); % I want this to be the string "Table_A" so that I can use it to name the heading of a table
%%%%% Now do things on the array "ID" during the for loop such as extracting data
Time= ID.Time;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
At the end of the for loop, I am duping the data into a table.
Is there any way to achevie what I want?
Thanks!

回答(2 个)

Walter Roberson
Walter Roberson 2021-6-21
datalist = named_variable(Table_A, Table_B, Table_C);
%stuff
for i = 1:numel(datalist)
ID = datalist(i).content;
Table_ID = datalist(i).name;
time = ID.Time;
end
function ds = named_variable(varargin)
ds = struct('content', varargin, 'name', cell(1,nargin));
for K = 1 : nargin
name = argname(K);
if isempty(name)
error('input #%d must be a named variable not an expression', K);
end
ds(K).name = name;
end
end
  2 个评论
James Browne
James Browne 2021-6-22
I tired something like that but the "datalist = named_variable(Table_A, Table_B, Table_C);" bit didn't work.
named_variable isn't a function. Can you elaberate on what is hapening here?
Stephen23
Stephen23 2021-6-22
编辑:Stephen23 2021-6-22
"Can you elaberate on what is hapening here?"
Poor data design is forcing you into writing complex, inefficient code that is difficult to work with.
The simple, efficient approach is to use indexing into one array, just as the MATLAB documentation shows:

请先登录,再进行评论。


Stephen23
Stephen23 2021-6-22
Your approach is leading you up the garden path. It is simpler to use indexing:
P = 'absolute or relative path to where the files are saved';
C = ["Table_A","Table_B","Table_C"];
N = numel(C)
D = cell(1,N);
for k = 1:N
F = fullfile(P,C{k});
D{k} = readtable(F,opts);
end
And now, exactly as you request:
for k = 1:numel(D)
ID = D{k}; % table of filedata
ID.Time
Table_ID = C(k); % table name
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by