Need help trying to figure out what the following notation means.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shawn
am 19 Okt. 2015
Bearbeitet: Stephen23
am 20 Okt. 2015
So, I am currently working on a project based on code written by someone else. Unfortunately, he didn't comment his code and is unavailable for any help. So my question is what is the following matlab notation?
for i=1:length(files)
eval(['load ' files(i).name]);
stuffs(i).name=files(i).name;
stuffs(i).h=bunchofh;
stuffs(i).nu=bunchofnu;
Like what is the stuffs(i).name syntax? Like, what is the name of such operation and what is it's purpose?
Thanks in advance for your help.
Sincerely, Shawn
1 Kommentar
Stephen23
am 20 Okt. 2015
Bearbeitet: Stephen23
am 20 Okt. 2015
This is awful code which uses eval for a trivial operation. Although beginners love using eval everywhere it is a very poor programming practice. Read this to know why:
Walter Roberson's answer shows the fast, efficient and robust way to perform this operation.
Akzeptierte Antwort
TastyPastry
am 19 Okt. 2015
It appears that stuffs is a structure array. The author is accessing the fields name, h, and nu in structure i in the array.
Weitere Antworten (1)
Walter Roberson
am 20 Okt. 2015
The code should be rewritten as
for i=1:length(files)
filedata = load(files(i).name);
stuffs(i).name = files(i).name;
stuffs(i).h = filedata.bunchofh;
stuffs(i).nu = filedata.bunchofnu;
and so on.
As TastyPastry correctly remarked, this is a structure array; see http://www.mathworks.com/help/matlab/structures.html
The changes I made had to do with avoiding eval(), which has a lot of security and efficiency problems. There are very few good reasons to use eval().
Siehe auch
Kategorien
Mehr zu Performance and Memory 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!