Loop different variables from workspace

num = 1; -------> 475
current_file = Test_1; -----------------> test_475 (Table of name 'Test_1' in workspace)
rate = height(current_file);
for i=1:rate
rel = current_file.S;
vel = current_file.E;
time = current_file.t;
Target_v{i,1} = rel(i) + vel(i);
if i == rate
T = array2table(Target_v);
C = [current_file(:,1:3) T current_file(:,4:45)]
eval(['Dataset_' num2str(num) '= C']);
Target_v= [ ];
end
end
I want to load multiple variables that are in workspace in a for loop add a column to each one of them and save them with another name or export them to excel. How can I do this?

4 comentarios

Matt J
Matt J el 16 de Dic. de 2019
Editada: Matt J el 16 de Dic. de 2019
Go back and make a single variable, instead of many, to hold the information.
Mariana
Mariana el 16 de Dic. de 2019
Editada: Mariana el 16 de Dic. de 2019
What do you mean by going back? Creating a structure? I currently have different mat files and on each one of them I have different variables in form of a table with similar name.
  1. First I need to access to the first mat file
  2. Load the first variable
  3. Add a new column of data
  4. Save it
  5. Next variable(loop)
  6. Next mat file
Stephen23
Stephen23 el 16 de Dic. de 2019
"I want to load multiple variables that are in workspace in a for loop..."
If they are already in the workspace then they have already been loaded (unless you wrote out all of those names by hand), so most likely the best choice would be to simplify your code and load the data into one array (e.g. a cell array, a structure, a table, etc.).
Note that having many numbered variables is a sign of badly designed data.
"...add a column to each one of them and save them with another name or export them to excel."
Easy with basic indexing into one variable.
Mariana
Mariana el 16 de Dic. de 2019
mat = dir('*.mat');
for q = 1:1
load(mat(q).name); I load the first mat file from a struct
----- it load all the tables in the workspace, but how do I modify each one of them? without using sprintf or eval.
end

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 16 de Dic. de 2019
Editada: Stephen23 el 16 de Dic. de 2019
The most important thing is to always load into an output variable:
Q = load(...) % Q is a scalar structure
where the fields of Q contain the variables from the .mat file. From there it is easy to access the fields using dynamic fieldnames:
As a special case, if each .mat file contains exactly one variable (e.g. a table) each with a different name, then you can use struct2cell quite effectively:
D = 'C:\Users\yourname\yourwork'; % absolute/relative path to where the files are saved
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
Q = load(fullfile(D,S(k).name));
C = struct2cell(Q);
T = C{1}; % T will be your table
... do whatever you want with T,
... then save it in a CSV file:
F = fullfile(D,sprintf('newfile_%d.csv',k));
writetable(T,F)
... or store it for accessing after the loop:
S(k).table = T;
end

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 16 de Dic. de 2019

Editada:

el 16 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by