how to use string in eval function
17 次查看(过去 30 天)
显示 更早的评论
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'.
0 个评论
回答(2 个)
Stephen23
2022-8-29
移动:Image Analyst
2022-8-29
"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']
or even better using SPRINTF:
sheet = sprintf('p%d_%s_final',x,y)
2 个评论
Image Analyst
2022-8-29
@Ham Man then please click the "Accept this answer" link to award @Stephen23 "reputation points". He'll appreciate it. Thanks in advance. 🙂
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Hamming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!