Can I name the variables in a table from a cell array or a struct?
32 ビュー (過去 30 日間)
古いコメントを表示
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
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:
採用された回答
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 件のコメント
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
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.Height
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!