insert variable k value into new variable names

5 ビュー (過去 30 日間)
Daniel Kazal
Daniel Kazal 2021 年 5 月 3 日
編集済み: Rik 2021 年 5 月 3 日
Hello,
I have a folder of tif files with sequential names (on_1, on_2, off_1, off_2, etc.) I have a script to process each trial set of images. Currently I manually change the value of all the image file names in the script.
Is there a way to simply create a variable k at the beginning of the script, where is automatically changes the value in the name of all my variables (and file names) in the script.
example:
k=1
on(k)=imread('on_(k).tif');
on(k)=double(on(k);
figure; imshow(on(k));
Thanks
  1 件のコメント
Stephen23
Stephen23 2021 年 5 月 3 日
"...automatically changes the value in the name of all my variables..."
Changing variable names dynamically is slow, inefficient, complex, liable to bugs, and difficult to debug.
The neat, simple, efficient solution is to use indexing, just as the MATLAB documentation shows:

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

回答 (1 件)

Rik
Rik 2021 年 5 月 3 日
Yes:
k=1;
on{k}=imread(sprintf('on_%d.tif',k));
on{k}=double(on{k});
figure; imshow(on{k});
The main point is to use a cell array if your elements are not the same size or data type.
  2 件のコメント
Daniel Kazal
Daniel Kazal 2021 年 5 月 3 日
編集済み: Rik 2021 年 5 月 3 日
Very helpful thanks.
Is there a way to name the images in the cell array? I suppose if they are sequential it does not matter.
How would you call upon one of the elements within the array.
say the second image of a 1x3.
Rik
Rik 2021 年 5 月 3 日
You should not name parts of variables. The file name is data and should be stored in a variable, not in the name of a variable.
You can create a new cell array that stores the names if you prefer to keep them around (e.g. if you had used dir to find all images in a folder).
You can access the variables the same way they are created here: on{2} will get the second element of the cell array on.

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by