Save new name in file .mat and loop new name

This is my code
function [] = AREAPICTURE (BW)
BWAREA = bwarea(BW);
save('AREA.mat','BWAREA');
end
I want have save new name of BWAREA sample BWAREA001 ,|BWAREA002| loop new name and save in file .mat or csv file.

2 commentaires

Stephen23
Stephen23 le 21 Jan 2017
Do not do this. Some other beginners will tell you to use eval or evalin, but this is bad advice. This makes slow and buggy code. The best solution is do not access variable names dynamically. Just because beginners keep on dreaming up the idea that accessing their variable names dynamically will solve all of their problems does actually not make this a good solution. It is not a good solution. Learn to write fast, robust, efficient code by using indexing, ND arrays, structures, tables, or strings.
Read this to know why using eval or evalin is a bad idea:
Adisorn Phanukthong
Adisorn Phanukthong le 21 Jan 2017
Modifié(e) : Adisorn Phanukthong le 21 Jan 2017
ok thank you,I'm begin Practicing matlab for project graduate in university and I never study mathlab. I learned by myself
I hope to write programming good same as you.

Connectez-vous pour commenter.

Réponses (2)

Jorge Mario Guerra González
Modifié(e) : Jorge Mario Guerra González le 21 Jan 2017
You mean, how to save each BWAREA variable in a different file using a loop when you have variables that follow a pattern? try this.
BWAREA001=1;
BWAREA002=1;
BWAREA003=1;
BWAREA004=1;
BWAREA005=1;
for i=1:5
variable=strcat('BWAREA00',num2str(i));
filename=strcat('AREA00',num2str(i));
save(filename,variable);
end
which saves

4 commentaires

Stephen23
Stephen23 le 21 Jan 2017
"The key is to use the function evalin"
The key to writing slow and buggy code.
The key to writing robust fast code is to learn how to use indexing properly.
I want save in array in mat file are many obj in this picture my code create in function use in main program in main program send a many pictue use in function this
Stephen23
Stephen23 le 21 Jan 2017
@Adisorn Phanukthong: see my answer for a more robust solution.
Jorge Mario Guerra González
Modifié(e) : Jorge Mario Guerra González le 21 Jan 2017
@Stephen Dobeldick It had an uncecessary use of evalin. I corrected it.

Connectez-vous pour commenter.

Stephen23
Stephen23 le 21 Jan 2017
Do not do this. Some other beginners will tell you to use eval or evalin, but this is bad advice. This makes slow and buggy code. The best solution is do not access variable names dynamically. Just because beginners keep on dreaming up the idea that accessing their variable names dynamically will solve all of their problems does actually not make this a good solution. It is not a good solution. Learn to write fast, robust, efficient code by using indexing, ND arrays, structures, tables, or strings.
Read this to know why using eval or evalin is a bad idea:
Although many beginners to do not seem to see this, it is much better to name your data with the same name. Put them in different files if you wish, but really each variable in your sequence of .mat files should be the same. Then saving and loading that data is really trivial (and does not require slow and buggy eval or evalin):
>> A = [1,2,3];
>> save('test1.mat','A');
>> A = [4,5,6];
>> save('test2.mat','A');
>> A = [7,8,9];
>> save('test3.mat','A');
and then to load that data is so easy:
>> D = dir('*.mat');
>> N = numel(D);
>> C = cell(1,N);
>> for k = 1:N, S = load(D(k).name); C{k} = S.A; end
>> C{:}
ans =
1 2 3
ans =
4 5 6
ans =
7 8 9
See how easy that is! No slow and buggy eval or evalin, no pointless functions, no wasting my time. When beginners want to learn how to write efficient MATLAB code then a good start is to learn how to avoid slow and buggy code that uses eval and evalin.

6 commentaires

I see in your post that you are the #1 AntiEval guy. I didn't know all those drawbacks. However, I have to disagree with you in the fact that evalin is completely useless.
Sometimes woking with GUIs you need function like thatone.
Image Analyst
Image Analyst le 21 Jan 2017
I agree with Stephen. I've used MATLAB for quite a long time and write GUIs constantly, and I've never ever had to use eval() even once in my code. However I'd be open minded if you give an example of where eval() is the only or best way to accomplish something.
Stephen23
Stephen23 le 21 Jan 2017
Modifié(e) : Stephen23 le 21 Jan 2017
@Jorge Mario Guerra González: although you might think that I am "the #1 AntiEval guy", consider this: I did not invent the idea that eval is a bad programming method. Programmers who were writing code before you or I were born did. I also did not write most of the opinions on eval: these were written by MATLAB experts who have much more experience than me: some of them work for TMW (who make MATLAB), some are professionals, some are retired after years of good work... but all of them have plenty of experience with writing code and fixing other people's code. And all of them say the same thing "avoid using eval".
I noticed that many beginners keep making the same bad program design decisions, so I collected all of the reliable references that I could find onto one page, and give links to anyone who might find this useful:
So instead of thinking of me as "the #1 AntiEval guy", think about all of those experts (like I said, much better than me or you) who consistently give this advice, and have written this advice a thousand times before, to every beginner who claims to have found eval nirvana. If you continue to read the links in my tutorial you will also find the occasional beginner who claims to know better than the accumulated knowledge of millions of person-hours of those experts (exactly how they know better is still a puzzle to me: perhaps they just feel it in their guts?).
I feel honored that I have had the opportunity to learn from these experts: that they share their knowledge, that it is available to me, and that I can gain some insight into how to use MATLAB efficiently, making my work better and more enjoyable. If you are not interested in using MATLAB efficiently, then by all means ignore all of their advice and continue to write buggy, slow code. It makes no difference to me. My code is faster and more efficient because of tools and methods that I have learned from those experts. Whether your code is efficient is not my problem. It is yours.
You might also like to consider this: different ways of writing code are not equally good. Do you think that an efficient way to write C++ would be the equally efficient when writing Python? That MATLAB should be written like HASKELL? The reality is that every language has its own paradigm, and its own ways that allow it to be used efficiently. In some languages this includes dynamically accessing variable names. In MATLAB (and quite a few others) using eval is incredibly inefficient, a security risk, and produces code that is almost impossible to debug.
Once you understand JIT a bit more I promise that you will never use eval again.
@Image Analyst, @Stephen Cobeldick Yes, of course all around there are a lot of experts that really master the art of coding and have so much more experience than I do. I will provide an example of a momement when I used evalin.
I would appreciate If you guys give another idea of how to do it.
var_name=inputdlg('Insert name of a valid matrix from the Workspace','Load matrix');
handles.a=evalin('base',var_name{:});
Those lines are part of a GUI that displays DICOM datasets. Imagine you have all your volumes in the command window and you want to pick one of those. I think you would say that just put them all in one variable but it's difficult to manage when you are used to name variable chest_scan, coronary_angiogram for instance.
Thus, the question is: How do you assign the value of a variable in the workspace to an object within handles
Should I open an new topic?
Thanks in advance.
Stephen23
Stephen23 le 21 Jan 2017
Modifié(e) : Stephen23 le 21 Jan 2017
"Imagine you have all your volumes in the command window and you want to pick one of those. I think you would say that just put them all in one variable but it's difficult to manage when you are used to name variable chest_scan, coronary_angiogram for instance"
Putting lots of variables into the base workspace and then try to access them using eval or evalin is never going to be efficient. If you wish to write efficient code then that is the design decision that should be changed. How did they get into the workspace? If they were loaded from files then it is easy to load them into one variable, from whence accessing by fields of indices is fast and efficient. This all repeats what has been written before, so please read the links in my tutorial
@Stephen Cobeldick, They were loaded through a function to load DICOM datasets.
matrixAA=loaddicom(<folder>);
I appreciate the tips on your tutorial, very well done and complete. Maybe I have to familiarize with other structs to acces to my variables instead of have a ton of them in the workspace.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by