Save new name in file .mat and loop new name

8 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 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 on 21 Jan 2017
Edited: Adisorn Phanukthong on 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.

Sign in to comment.

Answers (2)

Jorge Mario Guerra González
Edited: Jorge Mario Guerra González on 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 Comments
Stephen23
Stephen23 on 21 Jan 2017
@Adisorn Phanukthong: see my answer for a more robust solution.
Jorge Mario Guerra González
Edited: Jorge Mario Guerra González on 21 Jan 2017
@Stephen Dobeldick It had an uncecessary use of evalin. I corrected it.

Sign in to comment.


Stephen23
Stephen23 on 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 Comments
Stephen23
Stephen23 on 21 Jan 2017
Edited: Stephen23 on 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
Jorge Mario Guerra González
Edited: Jorge Mario Guerra González on 21 Jan 2017
@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.

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!