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

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

I did not follow the answer completely. I wish to make a general script where I can merge a set of 4 consecutive doubles and than delete the later 3.
Stephen23
Stephen23 am 25 Jan. 2018
Bearbeitet: Stephen23 am 25 Jan. 2018
" I wish to make a general script where I can merge a set of 4 consecutive doubles and than delete the later 3."
I understand what you want to do. Read my answer carefully, it is relevant.
How do those four variables get into the workspace? Are there always four of them, or do you want this to work with N variables?
They are imported to the workspace from a .dat file and yes they are always in pair of 4.
I 100% agree with Stephen that it's a horrible idea. To do the specific task at hand, simply do
abc_1 = [abc_1, abc_2, abc_3];
clear('abc_2', 'abc_2');
But doing it for some general case where you put in some wildcard name as a string is a bad idea.
I need to combine variables in pair of 4 and the number of pairs and their names can differ depending on the .dat file so the above simple nice solution might not work all the time
Stephen23
Stephen23 am 25 Jan. 2018
Bearbeitet: Stephen23 am 25 Jan. 2018
"I need to combine variables in pair of 4 and the number of pairs and their names can differ depending on the .dat file ..."
How do you do that? The standard functions for importing data into the MATLAB workspace do not magically create different variable names, so either you are performing some magic to get those variable names, or using someone else's slow and buggy tool that magically spams those variables into the workspace.
Question: how did you get lots of differently named variables into the workspace?
You should really fix this problem at the source (and that means improving how the data is imported), rather than trying to write slow hack code to magically play around with variable names.
The data I am importing does not necessarily have the same variable names because I work on diff areas of engine calibration and the variable names differ depending on which area i am working on.
The data I am importing does not necessarily have the same variable names because I work on diff areas of engine calibration and the variable names differ depending on which area i am working on.
That is irrelevant. The problem is that the process you're currently using to import the data into matlab is not good. We're trying to help you fix that process but until you tell us what it is, we can't.
I regularly import hours of engine test data into matlab. I never have any problem manipulating that data because I use an efficient import method. One that does not create random variable names.
Stephen23
Stephen23 am 26 Jan. 2018
Bearbeitet: Stephen23 am 26 Jan. 2018
@harshpurohit11: here is my question again:
"Question: how did you get lots of differently named variables into the workspace?"
I did not ask about engine calibration or whether the variable names might be different. I asked how you are importing this data into MATLAB workspace: are you using load, or any standard MATLAB function or tool, or some third-party function/app/tool... ?
I am using mdfimport. I was trying to make you understand that I cannot simplify the script the way you suggested bcz it would not suffice for all my requirements.
@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?
"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 MATLAB Parallel Server finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 25 Jan. 2018

Bearbeitet:

am 15 Mär. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by