How can I call a variable based on for loop count?

8 次查看(过去 30 天)
I'm trying to create a for loop that goes through images and gets the average signal value in a preset rectangle. I've labeled each image as im1_abs, im2_abs, im3_abs, etc., but can't seem to figure out how to use the the loop count 'n' to call each image.
Any tips on how to do this or easier ways to go about it are appreicated.
Below is the code I was trying to work through, with **n** where I was hoping to have the loop count. Dvar is a seperate file that essentially prints the value of the variable. Thanks!
  2 个评论
Dyuman Joshi
Dyuman Joshi 2024-2-15
编辑:Dyuman Joshi 2024-2-15
"I've labeled each image as im1_abs, im2_abs, im3_abs, etc., but can't seem to figure out how to use the the loop count 'n' to call each image."
DO NOT name variables dynamically. That is explained in length in this tutorial - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)
"Any tips on how to do this or easier ways to go about it are appreicated."
Store the data in an array and use indexing to access the data. Refer to the tutorial on how to do so and more information as well.
Stephen23
Stephen23 2024-2-15
编辑:Stephen23 2024-2-15
"I've labeled each image as im1_abs, im2_abs, im3_abs, etc."
And that is the cause of your problem. Because trying to access variables names dynamically forces you into writing slow, complex, inefficient, obfuscated, inscure, buggy code that is hard to debug:
"...but can't seem to figure out how to use the the loop count 'n' to call each image."
Don't do that.
"any tips on how to do this or easier ways to go about it are appreicated."
MATLAB is designed to work with arrays. It is simple and efficient to access the content of arrays using indexing.
You should use an array (e.g. a cell array, a structure array, an ND array) and indexing. Indexing is simple and efficient (unlike what you are trying to do).

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by