フィルターのクリア

I want to name variables while writing into matfile in a for loop

2 ビュー (過去 30 日間)
Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 7 日
コメント済み: Walter Roberson 2018 年 12 月 7 日
I have a loop over dates and all the value(mostly cell arrays) comes out from this loop is going to be saved into matfile. But I want to name those variables by this dates I've been looping over. You can see this part of the code below.
m = matfile('myFile.mat','Writable',true);
d1=datenum(dateStart);
d2=datenum(dateEnd);
for Date = d1:d2
...
....
m.datestr(Date) = outCell;
end
but obviously that doesn't work. It says "To index into the new variable 'datestr', specify at least two dimensions. MatFile objects do not support linear indexing".
  2 件のコメント
Stephen23
Stephen23 2018 年 12 月 7 日
編集済み: Stephen23 2018 年 12 月 7 日
That is very bad data design. Using serial date numbers as an index:
  1. will likely face numeric issues with the floating point numbers used.
  2. is an inefficient waste of memory: consider that today (for example) has the serial date number 737401: this means that to store just one data value you would have to create an array with nearly one million elements in it!
Using serial date numbers as indices is tantamount to putting meta-data into variable names or fieldnames: meta-data is data, and so it should be stored as data, not as indices.
"But I want to name those variables by this dates I've been looping over"
That is very bad data design. Meta-data, such as your dates, should not be forced into variable names or fieldnames. Read this to know why:
Instead, you should keep your data in arrays and use efficient indexing. Simply storing the data in two arrays with corresponding elements would likely be the simple MATLAB solution:
dates = [D1,D2, ..., Dn]
data = {C1,C2, ..., Cn}
Or you could use a table. Or a structure:
S(1).date = D1
S(1).data = C1
...
S(n).date = Dn
S(n).data = Cn
All of those would be easy to define in a loop, or at once using table, struct, etc.
Omer Yasin Birey
Omer Yasin Birey 2018 年 12 月 7 日
Thanks Stephen your answer is quite helpful. However, Im taking the dates as inputs from user. So, I only have 2 variables(about dates) which are start and end. The only thing that follows the dates, is the index of the loop. That's why it feels like it has to be dynamic.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 12 月 7 日
Do the datestr(Date) and store the string into a variable . Then use dynamic field naming .
m.(date_string) = values
You do not really need to use aa temporary variable but it helps make code clearer .
  6 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 7 日
You could also consider using containers.Map for fast access .
Walter Roberson
Walter Roberson 2018 年 12 月 7 日
Using this approach of dynamic field names is probably not the best, but it can have its uses, especially when there are external interface requirements.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by