how to use string in eval function

9 Ansichten (letzte 30 Tage)
Ham Man
Ham Man am 29 Aug. 2022
Kommentiert: Image Analyst am 29 Aug. 2022
I'd like to use eval for this expression: sheet ='p1_Q1_test';
It works for num2str(x) but does not work for string y.
How can I use the string 'y' correctly in eval?
x = 1;
y = 'Q1';
eval(['sheet =p' num2str(x) '_' y '_final']);
A = xlsread('E:\myfolder\datasheet.xlsx', sheet);
error:
Undefined function or variable 'p1_Q1_test'.

Antworten (2)

KSSV
KSSV am 29 Aug. 2022
x = 1;
y = 'Q1';
sheet =strcat('P',num2str(x),'_',y,'_final')
sheet = 'P1_Q1_final'

Stephen23
Stephen23 am 29 Aug. 2022
Verschoben: Image Analyst am 29 Aug. 2022
"I'd like to use eval for this expression: sheet ='p1_Q1_test';"
Why write such complex code?
x = 1;
y = 'Q1';
sheet = ['p',num2str(x),'_',y,'_final']
sheet = 'p1_Q1_final'
or even better using SPRINTF:
sheet = sprintf('p%d_%s_final',x,y)
sheet = 'p1_Q1_final'
  2 Kommentare
Ham Man
Ham Man am 29 Aug. 2022
Verschoben: Image Analyst am 29 Aug. 2022
Great thanks. It works nice!
Image Analyst
Image Analyst am 29 Aug. 2022
@Ham Man then please click the "Accept this answer" link to award @Stephen23 "reputation points". He'll appreciate it. Thanks in advance. 🙂

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by