필터 지우기
필터 지우기

Help with Simplifying repetitive code

조회 수: 5 (최근 30일)
Ines Shekhovtsov
Ines Shekhovtsov 2023년 1월 20일
답변: Kevin Holly 2023년 1월 20일
I am looking for a way to reduce writing repetative code and perhaps use for loops instead. I've been told that using eval is not the best approach when using a for loop to try and simplify the below code. Does anyone have any other suggestions/code snippets on how i can streamline this:
RD_ROI=RD(timeLimits(1):timeLimits(2));
RIC_ROI=RIC(timeLimits(1):timeLimits(2));
RLT_ROI=RLT(timeLimits(1):timeLimits(2));
ROB_ROI=ROB(timeLimits(1):timeLimits(2));
RPEC_ROI=RPEC(timeLimits(1):timeLimits(2));
RPS_ROI=RPS(timeLimits(1):timeLimits(2));
RRA_ROI=RRA(timeLimits(1):timeLimits(2));
RUT_ROI=RUT(timeLimits(1):timeLimits(2));
Basically i'm creating new variables with _ROI that take existing variable and extract a region of interest.
Thank you in advance for your time!

답변 (1개)

Kevin Holly
Kevin Holly 2023년 1월 20일
If you have your variables as columns in a table, you could do something like this:
t=table;
t.RIC = rand(14,1);
t.RLT = rand(14,1);
t.ROB = rand(14,1);
t.RPEC = rand(14,1);
t.RPS = rand(14,1);
t.RRA = rand(14,1);
t.RUT = rand(14,1);
t
t = 14×7 table
RIC RLT ROB RPEC RPS RRA RUT ________ ________ ________ ________ ________ _______ _______ 0.080879 0.25474 0.48853 0.095937 0.66441 0.58813 0.18798 0.22866 0.053753 0.5189 0.047562 0.66939 0.29408 0.3805 0.2994 0.97273 0.51125 0.80037 0.9816 0.27859 0.68301 0.64157 0.95801 0.31506 0.0599 0.82694 0.28359 0.10281 0.25327 0.65621 0.27398 0.77928 0.054263 0.74135 0.17095 0.99553 0.74094 0.072548 0.50858 0.23869 0.44027 0.6492 0.92099 0.048788 0.94306 0.1584 0.81534 0.96358 0.39643 0.16671 0.92683 0.012261 0.72819 0.35723 0.61379 0.47284 0.68682 0.57082 0.82924 0.33191 0.43374 0.13102 0.16932 0.94069 0.9905 0.50238 0.25963 0.089702 0.9822 0.483 0.25529 0.54532 0.2677 0.51884 0.3802 0.18224 0.63096 0.030474 0.27753 0.93547 0.3527 0.98646 0.3895 0.35703 0.30422 0.076089 0.97177 0.78907 0.23365 0.86461 0.40572 0.59585 0.068501 0.40167 0.62403 0.80161 0.81885 0.44789
timeLimits(1) = 3;
timeLimits(2) = 10;
ROI=timeLimits(1):timeLimits(2);
t_ROI = t(ROI,:)
t_ROI = 8×7 table
RIC RLT ROB RPEC RPS RRA RUT _______ ________ ________ _______ ________ _______ _______ 0.2994 0.97273 0.51125 0.80037 0.9816 0.27859 0.68301 0.64157 0.95801 0.31506 0.0599 0.82694 0.28359 0.10281 0.25327 0.65621 0.27398 0.77928 0.054263 0.74135 0.17095 0.99553 0.74094 0.072548 0.50858 0.23869 0.44027 0.6492 0.92099 0.048788 0.94306 0.1584 0.81534 0.96358 0.39643 0.16671 0.92683 0.012261 0.72819 0.35723 0.61379 0.47284 0.68682 0.57082 0.82924 0.33191 0.43374 0.13102 0.16932 0.94069 0.9905 0.50238 0.25963 0.089702 0.9822 0.483

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by