Can I name the variables in a table from a cell array or a struct?
23 views (last 30 days)
Show older comments
Jan Böttner
on 26 Jul 2023
Commented: Jan Böttner
on 27 Jul 2023
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
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:
Accepted Answer
Image Analyst
on 26 Jul 2023
Just to add to @Cris LaPierre answer, to process a bunch of files you can adapt code snippets in FAQ:
Once the variables are in a table, t, you can get the variable names like
t = readtable(fileName)
variableNames = t.Properties.VariableNames;
To set (change) the variable names to some custom strings that you'd rather have, just assign that property to a cell array
t.Properties.VariableNames = {'Column1', 'SomeStuff', 'OtherMeasurement', 'TimeOfDay'}
or whatever you want.
2 Comments
Stephen23
on 27 Jul 2023
Edited: Stephen23
on 27 Jul 2023
"The result files come from TRNSYS and are saved with custom fileending (enerbal, enerbaltank, enerballosses). Those are not recognized by Matlab, so the readtable function does not work."
The file extension is completely irrelevant to what READTABLE can import: that some file extensions are used to automatically determine the file format does not mean that other file extensions are somehow not possible to import. If the file format is something suitable then you just need to specify the filetype:
Using the inbuilt options is a much better approach than fiddling around and reinventing everything yourself.
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
0 Comments
See Also
Categories
Find more on Text 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!