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

조회 수: 8 (최근 30일)
Camille
Camille 2024년 2월 15일
편집: Stephen23 2024년 2월 15일
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