Can I name the variables in a table from a cell array or a struct?
Show older comments
Hi!
I am loading a bunch of .txt files into Matlab and the data is stored in cell arrays (I found a tutorial that used this way).
The files contain the results of a simulation with variing parameters and there are 30 different values for each run, stored in 3 different files for each run.
After importing the files I want to copy the values from the three different files into one table (I got this to work).
Now I want to name the variables in the table and preferably with the same names as in the result files as those might be changed by the user.
Is there a way to read the names from the cell array and write it into the table without specifing it for each entry from the cell (data{1,1}, data{1,2}, ...? Or alternatively from a struct, as i managed to copy the names into a struct (name.a, name.b,...)?
Thanks!
3 Comments
Star Strider
on 26 Jul 2023
MATLAB has functions that can combine table arrays as well as name the additional variables with the names of the original table arrays. See Join Tables and the links in that page to see if one of them will do what you want.
Image Analyst
on 27 Jul 2023
You forgot to attach the file.
And you say they're *.txt files in your original post, but then in a comment below you say they're *.enerbal, *.enerbaltank, and *.enerballosses files. Which is it? If they're not .txt files, then zip up a few of them and attach the zip file, otherwise just attach the .txt files.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Jan Böttner
on 27 Jul 2023
Accepted Answer
More Answers (1)
Cris LaPierre
on 26 Jul 2023
Moved: Cris LaPierre
on 26 Jul 2023
Use readtable to toad your files, and the header names will be used automatically as the table variable names.
data = readtable('patients.xls')
data.Height
Categories
Find more on Workspace Variables and MAT Files 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!