Hi,
I have a problem with naming a variable during a for loop. I want to change the variable name in each iteration, so I use eval function for naming like this
dataset=rand(3);
for i=1:N
eval(['NAME_' num2str(i) '=dataset']);
end
But with eval function I always have out put on command window. Does anyone knows better solution for naming or not to show the output of eval function?
Thanks,

댓글 수: 2

Stephen23
Stephen23 2018년 6월 28일
편집: Stephen23 2018년 6월 28일
JAMES WAITE
JAMES WAITE 2019년 4월 11일
@Stephen Brimhall: found your resource helpfull for my issue. Thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 4일

0 개 추천

댓글 수: 2

Joseph Cheng
Joseph Cheng 2014년 9월 4일
편집: Joseph Cheng 2014년 9월 4일
In addition to Walter's post you could go:
dataset=rand(3);
for i=1:N
Name{i} =dataset;
end
Which is equivalent to your Name_# variable. Instead of working with Name_# you would type Name{#} instead. Note the {} bracket type as these are cells. Using cells also give the ability to have different sizes of dataset. Also with the Name{#} convention you do not have to hard code or use eval again to reprocess the data. For instance if there needs to be a scaling of all the data in Name you would have to hard code Name_1*2, Name_2*2, etc. you could write a for loop to do it and without using eval.
Byron Uzoka
Byron Uzoka 2020년 7월 11일

🤔

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sirshendu Mondal
Sirshendu Mondal 2020년 5월 4일

4 개 추천

Instead of
eval(['NAME_' num2str(i) '=dataset']);
use
eval(['NAME_' num2str(i) '=dataset;']);

댓글 수: 5

Instead of
eval(['NAME_' num2str(i) '=dataset']);
use
Name{i} = dataset;
and with less effort you have simpler, more efficient code!
Sirshendu Mondal
Sirshendu Mondal 2020년 5월 4일
This is truely nice. However, this is going to creat a single cell of very big size (maybe). Due to size restriction, one is sometimes forced split the variable and post process that one by one. I have experinced that during image processing.
Stephen23
Stephen23 2020년 7월 11일
편집: Stephen23 2020년 7월 11일
"However, this is going to creat a single cell of very big size (maybe)."
Not unless you have millions of tiny numeric arrays (in which case they should probably be stored in one numeric array anyway). The cell array itself is just a header of links to other data arrays, so really the cell array just adds a tiny overhead, either 60 bytes (for 32 bit OS) or about 112 bytes (for 64 bit OS) per cell, as the documentation explains:
On top of that is the circa 104 bytes of meta-data that every MATLAB array has. The overhead and the fact that the contents of cell arrays are not stored in contiguous memory is also explained in the MATLAB documentation:
Because the contents of the cell array are not stored contiguously it really makes very little difference to memory consumption, because they are allocated memory in much the same way as separate arrays are (because they are separate arrays).
"I have experinced that during image processing."
Images tend to be moderately large arrays, and it is common that not all images in a data set can be loaded into memory simultaneously. Because the image size (e.g. millions of bytes) is likely to be much larger than the cell array header (a few hundred bytes) then storing the arrays separately vs. in a cell array won't make much difference at all.
David Valentine
David Valentine 2022년 11월 22일
Thank you so much for this anwer(and the querier for that matter)! I'm working on a project that needed something like this, and all I could find was people saying to make either a structure or double array.
Stephen23
Stephen23 2022년 11월 23일
편집: Stephen23 2022년 11월 23일
"...all I could find was people saying to make either a structure or double array."
You can't have done very much reading on this topic if that was "all" the advice you found.
The advice given on this forum is much more nuanced and situation specific:

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2012년 6월 4일

편집:

2022년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by