Variable name from a cell content

26 views (last 30 days)
Luca D'Angelo
Luca D'Angelo on 14 Sep 2022
Edited: Stephen23 on 14 Sep 2022
Hi all,
I read many times that this it shouldn't be done but I can't find a way.
This is the situation. I have an excel file with about 10 different sheets. Each sheet contains different parameter values from different sampling sites (like Milan, Rome, Naples, Paris,....). The number of the parameters are different for each site but what is similar is that the first column (variable) is a datetime; the others are (fortunately) doubles.
I import the excel files like this:
[~,sheet_name]=xlsfinfo("filename.xlsx");
for k=2:numel(sheet_name) %I skip the first sheet in this case
[~,~,data{k}]=xlsread("filename",sheet_name{k});
end
Now I have "sheet_name" (cell) that contains the name of each sheet (=the name of each sampling site) and "data" (cell) that contains 19 cells, each contains a different variable numbers as string (I guess) because in the first row there are the variable name, in the first columns there are the datetime (but as string, I guess) and from 2:2 to end:end there are data that are supposed to be double.
So, I need to create a table (as Table: not cell, please) named as the corrisponding sampling site for each sampling site.
Any idea about what it would be the best approach to obtain this?
Thank you in advance.
Luca
  4 Comments
Stephen23
Stephen23 on 14 Sep 2022
Edited: Stephen23 on 14 Sep 2022
"I read many times that this it shouldn't be done..."
For very good reasons:
"...but I can't find a way."
What is your actual goal? Tell us about your actual goal, not about your attempted approach:
Most likely your goal is not "create lots of separate variables in the MATLAB workspace", but is actually something like "loop over all sheets and create a plot from each one", or something similar. Tell us what your goal is.
"So, I need to create a table (as Table: not cell, please) named as the corrisponding sampling site for each sampling site."
I doubt that you actually "need" to make accessing your data slow, inefficient, and more complex.
Luca D'Angelo
Luca D'Angelo on 14 Sep 2022
I thank you for your interest.
For a screening phase I could only be interest in do some plots (why not?) but since I might need to do some other statistical analysis, it would be really nice to can call data in a easy way.
Whether I need to do some plots only, I could realize them with Excel....
And anyway, I'm not that confortable with "cell" to plot or analyse data whereas I think that "Tables" are great!

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 14 Sep 2022
Edited: Stephen23 on 14 Sep 2022
You could use a structure to store all of the tables:
F = "Input.xlsx";
S = sheetnames(F);
D = struct();
for k = 1:numel(S)
T = readtable(F, 'Sheet',S{k});
D.(S{k}) = T;
end
D
D = struct with fields:
Roma: [168×5 table] Pistoia: [168×10 table] Condofuri: [168×11 table] Aosta: [168×7 table]
This assumes that the worksheet names are valid fieldnames:
Note that you could easily combine these into one table, by adding a column e.g. "site" and then concatenating.
"And anyway, I'm not that confortable with "cell" to plot or analyse data whereas I think that "Tables" are great!"
Tables are great, that is not the issue here.
Using a cell array and basic indexing would be simpler than messing around with variable names or field names.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!