Save new name in file .mat and loop new name

7 ビュー (過去 30 日間)
Adisorn Phanukthong
Adisorn Phanukthong 2017 年 1 月 21 日
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 件のコメント
Stephen23
Stephen23 2017 年 1 月 21 日
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 2017 年 1 月 21 日
編集済み: Adisorn Phanukthong 2017 年 1 月 21 日
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.

サインインしてコメントする。

回答 (2 件)

Jorge Mario Guerra González
Jorge Mario Guerra González 2017 年 1 月 21 日
編集済み: Jorge Mario Guerra González 2017 年 1 月 21 日
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 件のコメント
Stephen23
Stephen23 2017 年 1 月 21 日
@Adisorn Phanukthong: see my answer for a more robust solution.
Jorge Mario Guerra González
Jorge Mario Guerra González 2017 年 1 月 21 日
編集済み: Jorge Mario Guerra González 2017 年 1 月 21 日
@Stephen Dobeldick It had an uncecessary use of evalin. I corrected it.

サインインしてコメントする。


Stephen23
Stephen23 2017 年 1 月 21 日
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 件のコメント
Stephen23
Stephen23 2017 年 1 月 21 日
編集済み: Stephen23 2017 年 1 月 21 日
"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
Jorge Mario Guerra González 2017 年 1 月 21 日
編集済み: Jorge Mario Guerra González 2017 年 1 月 21 日
@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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by