Concatenate integer to variable name
Show older comments
Hi, I would like to concatenate the number of the corresponding level to the cell arrays I create. This is what I have tried so far along with other things but nothing seems to work.
for j= 1:230
for i=1:8
for lvl = 3:5
strcat([haar_decomp_ext{j,i}, haar_coeff_num_ext{j,1}],lvl) = wavedec(Ext{j}(:,i+1),lvl,'haar');
end
end
end
Thanks in advance.
8 Comments
Stephen23
on 29 Jun 2017
Do not do this. The best solution is to avoid creating or accessing variable names like that. 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):
Ioannis Agalliadis
on 29 Jun 2017
"What you recommend me is not to use cell arrays but instead the regular arrays?"
You should always put your data into the simplest array possible, because this makes your code simpler, more efficient, and much less buggy. If your data fits nicely in some numeric vector/matrix/ ND array then you should use that. If you have "ragged" data then a cell array may be appropriate. A structure can be used if you need to pass many parameters and wish to access them by name.
Most likely you can use a basic numeric matrix or ND array for the example that you show, depending on what wavedec is or what output it has (you did not tell us).
Ioannis Agalliadis
on 29 Jun 2017
Depends on what you need to do with it...which you've not provided sufficient information about to know, precisely.
But, with the same number of columns, presuming they're all numeric, then a double 2D array will hold them all which is the simplest storage mechanism and therefore fastest and most memory efficient. If your case is to process all, then single expressions over that array compute things like averages or math expressions directly on all elements in a single expression.
If it is needed to process each of these subsets somehow individually, then once you've processed a set is there any reason to keep it in memory any longer or just the result? If the former, then still a 2D array is most efficient; simply save the result(s) before bringing in the next.
You can, of course, create a cell array that contains each of these 2D arrays as one of its elements, but to do that just make an array, don't create new numbered variables. Or, you could, as the reference documentation shows, use structures or a more recent alternative of the table. In a table you could/would create an additional grouping variable for each of the subsets that could be use to do calculations by each.
There are many choices; which is best depends on what objective is, but none use sequentially-named variables. That isn't to say there aren't places where eval has it uses but this is not one of those... :)
Ioannis Agalliadis
on 30 Jun 2017
Edited: Ioannis Agalliadis
on 30 Jun 2017
dpb
on 30 Jun 2017
As above, it depends on what you've left out...what do you want/need to do with the data when you've gotten it?
If each of these is an independent event, then it may make sense to keep them segregated by using the cell array for each.
But, as outlined, that can still be so but if you're just analyzing each in sequence, then there's not necessarily any reason to keep them all in memory so might as well just use the 2D array for each then proceed to the next.
Still not enough detail to know...
Accepted Answer
More Answers (1)
Steven Lord
on 30 Jun 2017
1 vote
"Every column belongs to a different channel and I have 8 columns of them. The first column is the time. The number of rows is different because the time of the movement that I am investigating varies. That means, that I have a variable number of rows always."
If you're using release R2016b or later, consider creating a timetable to store your channel data, using synchronize to merge each new channel into the timetable that will store all your data. If one channel has data at a time another does not, you can have synchronize fill the second channel's entry for that time with missing (for floating-point data, NaN.)
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!