how to modify a double whose name is stored as a string in another cell

3 Ansichten (letzte 30 Tage)
harshpurohit11
harshpurohit11 am 25 Jan. 2018
Bearbeitet: Stephen23 am 15 Mär. 2019
abc_1 = [1;2;3;4]; abc_2 = [2;3;4;6]; abc_3 = [3;5;6;7];
merge_all = who('*abc*');
para_num = 1;
while para_num<=length(merge_all)
try merge_all(para_num)=horzcat(merge_all(para_num),merge_all(para_num+1),merge_all(para_num+2))
clearvars merge_all(para_num+1) merge_all(para_num+2)
end
end
Basically I am trying to merge abc_1, abc_2 and abc_3 into abc_1 and than delete abc_2 and abc_3. Not sure how am I suppose to call them in the while loop

Antworten (1)

Stephen23
Stephen23 am 25 Jan. 2018
Bearbeitet: Stephen23 am 25 Jan. 2018
"Basically I am trying to merge abc_1, abc_2 and abc_3 into abc_1 and than delete abc_2 and abc_3. Not sure how am I suppose to call them in the while loop"
Basically what you are writing is slow, buggy, complex code. Making this easy would encourage people to use it, which would make their code slow, buggy, insecure, pointlessly complex, and harder to debug. Deciding to use numbered variables is how some beginners force themselves into writing slow code. But you don't have to do this.
Your best solution is to avoid this situation entirely: do not magically access variable names using slow introspective methods (e.g. who, etc). There is no reason why you should have numbered variables in your workspace: whether loading the data from file or generating it in a loop it is trivial to put all of that data into one array using indexing. Indexing is simple, neat, easy to debug, and very very efficient (unlike what you are trying to do). Read the links at the bottom of this post to see examples of how to avoid this situation.
You would do well to consult the relevant parts of the documentation: "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. The preferred method is to store related data in a single array"
Note that accessing is just as slow as creating those variables, for the reasons discussed in the documentation, the blogs, and this forum. You might as well read some of the discussions on this topic:
or any of the other countless times that this has been discussed before.
  12 Kommentare
harshpurohit11
harshpurohit11 am 26 Jan. 2018
@Guillaume how do you deal with different raster rates for the same imported channels and how do you create common variable names if the raster rates change?
Stephen23
Stephen23 am 26 Jan. 2018
Bearbeitet: Stephen23 am 15 Mär. 2019
"I am using mdfimport."
I will assume that you mean this FEX submission, which has a file named mdfimport. Unfortunately the function relies entirely on assignin to get the data into the workspace: this is the cause of your problems: it is badly designed code which forces you to write slow, ugly, complex hack code as a workaround for its design flaw.
You should note that the author wrote in a comment "Sorry, but I'm not maintaining this submission any more. MDF import is now provided in the Vehicle Network Toolbox: https://www.mathworks.com/help/vnt/mdf-file-support.html"
So you would be much better off using the (better written) tools in the Vehicle Network Toolbox.
Or simply download this FEX submission:
which lets you import into one output variable:
data = importMDF3(...)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by