How to save multiple Tables in Workspace with different names

16 views (last 30 days)
First of all I know i should not be doing this as every answer ist something like "you should use indexing" but i need it.
So, i got hundreds of tables with over 100'000 rows of data in 37 Variables and i have to import them (.csv files) into matlab. I wanted to use a for loop to do this. As the question stated i am not able to get the table saved into my workspace as the table name is not dynamically. Is there any good solution for this? I am not a programmer and i do not have much experience with matlab. What i try is to geht the names for the tables out of "name_cell" and change the "messungen" dynamically with the names i extract from there in some way. I just cannot get this code run. Appreciate every bit of help.
Here is the code i wrote so far:
dinfo = dir('/MATLAB Drive/Nur motor');
names_cell = {dinfo.name}
for i = 3:length(names_cell)
opts = detectImportOptions(names_cell{i});
opts.VariableNamesLine = 11;
opts.DataLines = [12 Inf];
messungen = readtable(names_cell{i},opts);
varname = names_cell{i}
varname = varname(1:end-4)
end
  5 Comments
Stephen23
Stephen23 on 9 May 2021
Edited: Stephen23 on 9 May 2021
"What i need is to save all the .csv files in seperate tables in my workspace."
The obvious, easy, simple solution is to use indexing, just like the documentation shows:
So far you have not provided any reason why that would not work for you. Something like this:
S = dir('/MATLAB Drive/Nur motor'); % even better: filename/extension matching.
S = S(~[S.isdir]); % correct way to ignore folders (your approach is buggy).
for k = 1:numel(S)
opts = detectImportOptions(S(k).name);
opts.VariableNamesLine = 11;
opts.DataLines = [12 Inf];
S(k).data = readtable(S(k).name,opts);
end
This will be much simpler and more efficient than your approach of saving multiple tables "with different names".

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!