Name variables according to string

1 回表示 (過去 30 日間)
Julian Pires
Julian Pires 2019 年 6 月 5 日
コメント済み: Stephen23 2019 年 6 月 6 日
Hello, my problem is as follows:
  • I have a series of folders named "100-'some text", "101-'blahblah'", "102-...", "103-..."
  • Each folder has a .txt file with columns of data used to generate a .mat file named "data"
  • These .mat files contain 1 struct named "data" with 2 fields: name & data. The names include 'Time', 'Speed', 'Temp', etc.
So I'd like to compare data between the folders by plotting Time vs. Temp for folder 100 and 101, but when I open the .mat files it overwrites the previous because they share the same name ("data").
What I'd like to do is open the .mat files and rename the signals according to the folder number. ie) "Time" will become "Time_100".
I know I should avoid using EVAL, but is there another way to do this? Or is there a way to avoid the problem entirely by naming the struct in each .mat file to "data_100"? I would prefer to extract the names in each struct, change them by appending the folder number, and then saving that workspace.
Thoughts?
Thanks
  1 件のコメント
Stephen23
Stephen23 2019 年 6 月 6 日
"I know I should avoid using EVAL..."
Yes, you should.
"..., but is there another way to do this?"
Of course: use an array (e.g. a structure array or a cell array) and indexing.
"Or is there a way to avoid the problem entirely by naming the struct in each .mat file to "data_100"?"
That does not avoid the problem, it just makes your code more complex! The simplest solution is when each .mat file contains variable using the same name, just as you described.

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

採用された回答

Rik
Rik 2019 年 6 月 5 日
Why not store them in a struct array? If all mat files have the same fields that should work very well.
  2 件のコメント
Julian Pires
Julian Pires 2019 年 6 月 6 日
Ideally, they would be kept in their own named folder as new data is added. I think I just need to rename the structures and their 'names' field with the folder number appended but I'm not sure how to rename the structure that way. Renaming the fields is easy
Stephen23
Stephen23 2019 年 6 月 6 日
" I think I just need to rename the structures..."
Rik's suggestion to use a structure array is simpler and much more efficient.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by