How to declare variable names from text file names?
2 次查看(过去 30 天)
显示 更早的评论
Hi, I am new to Matlab. I have some text files named as a_201706280202.txt, a_201706280402.txt, a_201706280204.txt, a_201706280404.txt, etc. Once they are loaded in the workspace, I want to call (name) them as variables such as A1, A2, B1, B2, etc. based on some criteria. For example, a_201706280202.txt will A1, because last and second last digits are 02 which refers to A and 4rth and 5th last is 04 which refers to 1. I will only use last 4 digits to name the variables. Similarly, A2=a_201706280402.txt (02 for A and 04 for 2), B1=a_201706280204.txt (04 for B and 02 for 1), B2=a_201706280404.txt (04 for B and 04 for 1). This the way how I want to continue for all of my files. Could anybody help me please? Thanks in advance.
2 个评论
Adam
2017-6-28
Use arrays instead (or structs if you really want, with dynamic string fields). Quite apart from the fact that, as you are finding, naming variables is difficult, that is only the start. Trying to use them afterwards is even worse.
If Stephen Cobeldick is around I'm sure he'll post his usual in-depth summary and links as to why not to do this, what to do instead, etc, etc, so I'll just leave it at my lazy paragraph above!
Stephen23
2017-6-28
编辑:Stephen23
2017-6-28
As predicted:
The most important question is: Why do these need to be stored as separate variables? The best solution is to avoid creating lots of variables. How? By using just one variable... and luckily it is trivial to avoid when loading/importing data or when creating data, by using indexing or fieldnames. So, given that it is so trivial to avoid this problem, when not improve your code by avoiding this whole problem altogether?
The name MATLAB comes from MATrix LABoratory: MATLAB is most efficient when data is kept together as much as possible (in vectors, in matrices, or in ND arrays) and the data accessed using indexing or by writing vectorized code. Like many other programming languages it is very inefficient to generate or access lots of separate variables.
About the worst solution would be to use eval (which some beginners love to use), you might like to first know what the MATLAB documentation says about it: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
You might also like to read what experienced MATLAB users say about what you are trying to do (hint: they strongly advise against it):
回答(1 个)
Rik
2017-6-28
If you are certain of what combination will exist, you may indeed use eval to fill the correct variables, but the rest of your could should not be dependent on it (i.e. only do this if you otherwise would need a monstrously large switch block). If not, use indexing (or in a pinch, dynamic field names, which are almost as terrible as dynamic variable names).
Why don't you use a cell array: data{2,2} would be A1, data{4,2} would be B1, etc. you can simply use this line of code to convert it:
filename='a_201706280202.txt';
data{ str2double(filename(11:12)) ,...
str2double(filename(13:14)) }=...
data_from_file;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!