简体   繁体   中英

Running a variable that is stored as string in matlab

Here is the deal, I have lots of variables that are stored in Excel. Those are the values that my Simulink model uses. What I want to do is to extract the descriptions of all of 'em automatically.

What I have done so far is the following,

I read them from column and defined a txt array to hold them as strings. Created another string as s2 to combine ".Description" string with variable names. I combined them and generated another array called descriptioner. Read the length from the txt array for counter. Used a small while loop to add .Description to all of them.

Where I am stuck is that I cannot run them and get the descriptions.

I used eval() but it fails. Is there a way to call those strings as variables to get the descriptions?

clc
filename = 'variables.xlsx';
[num, txt] = xlsread('variables','A:A')
L=length(txt)
s2='.Description'
i=1;
while i<=L
    descriptioner(i) = strcat(txt(i),s2)
    i=i+1;
end

Try allocating your descriptioner variable as a cell before doing your loop. Here's one way to try:

clc
filename = 'variables.xlsx';
[num, txt] = xlsread('variables','A:A');
descriptionsCell = cell(size(txt));
for i=1:numel(txt);
   descriptionsCell{i} = [txt{i},'.Description'];    
   fprintf(1,'The description of %s is %s\n',txt{i},eval(descriptionsCell{i}));
end

Let me know if this works or not. It works on my machine for a simulated example, but I'm always learning new things.

为了执行String中给出的MATLAB命令,应使用eval()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM