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

25 ビュー (過去 30 日間)
Jan Böttner
Jan Böttner 2023 年 7 月 26 日
コメント済み: Jan Böttner 2023 年 7 月 27 日
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 件のコメント
Image Analyst
Image Analyst 2023 年 7 月 27 日
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 2023 年 7 月 27 日
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.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2023 年 7 月 26 日
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 件のコメント
Jan Böttner
Jan Böttner 2023 年 7 月 27 日
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 2023 年 7 月 27 日
編集済み: Stephen23 2023 年 7 月 27 日
"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.

サインインしてコメントする。

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 7 月 26 日
移動済み: Cris LaPierre 2023 年 7 月 26 日
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

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by