Can I name the variables in a table from a cell array or a struct?

23 vues (au cours des 30 derniers jours)
Jan Böttner
Jan Böttner le 26 Juil 2023
Commenté : Jan Böttner le 27 Juil 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 commentaires
Image Analyst
Image Analyst le 27 Juil 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
Jan Böttner le 27 Juil 2023
Yes, my bad. I'm sorry, I didn't submit a good question. It got late yesterday and I was very annoyed trying all day and not getting my Script to work...
But I got it to work now with thanks to your replies!
I'm only struggeling with adding two tables together, as the join function does not work. But I'll submit another question for that.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 26 Juil 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 commentaires
Jan Böttner
Jan Böttner le 27 Juil 2023
THanks for the replies, unfortunatly I haven't expressed my problem well enough.
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.
And importing all the data and copying into a new table is not the problem, I got that to work.
Maybe the question is more general: Is it possible to name the Values in a table from variables or do I have to name them specifically? Then I can store all the names from the file in Variables and then write them into the heading of the table.
So instead of
t.Properties.VariableNames = {'Column1', 'SomeStuff', 'OtherMeasurement', 'TimeOfDay'}
it would be convinient to use
a = 'Column1'
b = 'SomeStuff'
c = 'OtherMeasurement'
d = 'TypeOfDay'
t.Properties.VariableName = {a, b, c, d}
if that is possible in any way.
Stephen23
Stephen23 le 27 Juil 2023
Modifié(e) : Stephen23 le 27 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 26 Juil 2023
Déplacé(e) : Cris LaPierre le 26 Juil 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 = 100×10 table
LastName Gender Age Location Height Weight Smoker Systolic Diastolic SelfAssessedHealthStatus ____________ __________ ___ _____________________________ ______ ______ ______ ________ _________ ________________________ {'Smith' } {'Male' } 38 {'County General Hospital' } 71 176 true 124 93 {'Excellent'} {'Johnson' } {'Male' } 43 {'VA Hospital' } 69 163 false 109 77 {'Fair' } {'Williams'} {'Female'} 38 {'St. Mary's Medical Center'} 64 131 false 125 83 {'Good' } {'Jones' } {'Female'} 40 {'VA Hospital' } 67 133 false 117 75 {'Fair' } {'Brown' } {'Female'} 49 {'County General Hospital' } 64 119 false 122 80 {'Good' } {'Davis' } {'Female'} 46 {'St. Mary's Medical Center'} 68 142 false 121 70 {'Good' } {'Miller' } {'Female'} 33 {'VA Hospital' } 64 142 true 130 88 {'Good' } {'Wilson' } {'Male' } 40 {'VA Hospital' } 68 180 false 115 82 {'Good' } {'Moore' } {'Male' } 28 {'St. Mary's Medical Center'} 68 183 false 115 78 {'Excellent'} {'Taylor' } {'Female'} 31 {'County General Hospital' } 66 132 false 118 86 {'Excellent'} {'Anderson'} {'Female'} 45 {'County General Hospital' } 68 128 false 114 77 {'Excellent'} {'Thomas' } {'Female'} 42 {'St. Mary's Medical Center'} 66 137 false 115 68 {'Poor' } {'Jackson' } {'Male' } 25 {'VA Hospital' } 71 174 false 127 74 {'Poor' } {'White' } {'Male' } 39 {'VA Hospital' } 72 202 true 130 95 {'Excellent'} {'Harris' } {'Female'} 36 {'St. Mary's Medical Center'} 65 129 false 114 79 {'Good' } {'Martin' } {'Male' } 48 {'VA Hospital' } 71 181 true 130 92 {'Good' }
data.Height
ans = 100×1
71 69 64 67 64 68 64 68 68 66

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by