Can I name the variables in a table from a cell array or a struct?
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jan Böttner
am 26 Jul. 2023
Kommentiert: Jan Böttner
am 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 Kommentare
Image Analyst
am 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:
Akzeptierte Antwort
Image Analyst
am 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 Kommentare
Stephen23
am 27 Jul. 2023
Bearbeitet: Stephen23
am 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.
Weitere Antworten (1)
Cris LaPierre
am 26 Jul. 2023
Verschoben: Cris LaPierre
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!