How to extract part of a variable name to make new variables in a loop?

3 Ansichten (letzte 30 Tage)
guamspider
guamspider am 6 Sep. 2017
Bearbeitet: Stephen23 am 6 Sep. 2017
I have a large number of matrices named with the form A1B5C1D3 where each letter and each number changes depending on where aspects of the data originates e.g. F7B5C1D3 or A1B6C2D3. I don't load directly from a directory because to get the data in a consistent format (same parameter in e.g. column 4) from the original files requires several conversions and calculations that I do within matlab. I want to do some basic filtering, various other calculations and plots, but always save the new matrix with its original name appended to the start. I am struggling to put a loop in place to run A1A1A1A1 through F1A1A1A1. After that I assume its similar to do A1A1A1A1 through A8A1A1A1 etc. I think I need to use the sprintf('%s_%d') command with a loop A through F and 1 through 8? I don't want to write it out 48 times!
An example of my code is below using A1B5C1D3 as an example:
A1B5C1D3Appendpoints = [Pointdata(:,1:3),A1B5C1D3]; A1B5C1D3OnlyifPoints=A1B5C1D3Appendpoints; A1B5C1D3OnlyifPoints(any(isnan(A1B5C1D3OnlyifPoints(:,2:3)),2),:)=[]; A1B5C1D3Difference = ((A1B5C1D3OnlyifPoints(:,3)- A1B5C1D3OnlyifPoints(:,12)).^2).^0.5;
  1 Kommentar
Stephen23
Stephen23 am 6 Sep. 2017
Bearbeitet: Stephen23 am 6 Sep. 2017
"I have a large number of matrices named with the form A1B5C1D3 where each letter and each number changes depending on where aspects of the data originates e.g. F7B5C1D3 or A1B6C2D3"
Well, that is the problem right there. Bad code design, putting meta-data into variable names. Beginners sometimes think that putting meta-data (e.g. indices or some test info) into variable names is a great idea. And then they wonder why they are faced with code that is slow, buggy, hard to debug, and hard to write.
If they simply used indexing then they would avoid all of these problems entirely.
Read this to know more about why putting meta-data into variable names is a bad idea, and why accessing variable names dynamically is a bad way to write code:
The better way to write code? Use indexing. Indexing is what I use, and what all experienced MATLAB users use... and we do not have the problems that you are trying to solve now: we avoid them entirely. Good code design makes it easier to write neat and efficient code:
"...but always save the new matrix with its original name appended to the start."
How on earth are you doing this?
"I am struggling to put a loop in place to run A1A1A1A1 through F1A1A1A1"
Well, it is no surprise that it is a struggle for you, because you picked the worst possible way to store data: in lots of separate variables and putting meta-data into their variable names. If you want to avoid the struggle just use simple indexing.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by