如何构造字符串并将其作为命令运行

时间:2017-09-19 17:54:29

标签: string matlab plot

我使用gramm绘制一些数据。 长话短说:我想根据用户输入构建一个字符串,然后将其作为调用gramm函数的命令运行。

我的结构包含许多样本的数据 - 每个样本中的数据包含许多化学物种的强度。

我构建了一个条形图,显示了每个样品的每种化学物质的强度。换句话说,我的y轴是强度,我的x轴是化学物种,我的颜色是'是样本。

现在我不想绘制每个样品甚至每个样品中的每种化学物质。就目前而言,我必须经历并对其进行硬编码。但是我想知道我想要绘制哪种化学物质和哪些样品。

截至目前,这意味着要写一个非常长的命令:

g=gramm('y', allData.(fileName).intensities, 'x', ...
    allData.(fileName).chemicalSpecies, 'color', ...
    allData.(fileName).sampleName,'subset',...
    allData.(fileName).sampleNum <u & ...
    allData.(fileName).sampleNum >l & ...
    allData.(fileName).chemicalSpecies =="Butanal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Butenal" |...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Pentanal" |...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Pentenal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Hexanal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Hexenal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Heptanal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Heptenal"| ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Octanal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Octenal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Nonanal"  | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Nonenal" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Butanedial"| ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Pentanedial"| ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Hexanedial"| ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Heptanedial"| ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Octanedial" | ...
    allData.(fileName).sampleNum <u & allData.(fileName).sampleNum >l ...
    & allData.(fileName).chemicalSpecies == "Nonanedial")

正如您所看到的,此代码仅绘制了某个样本子集(在samplenum u和l之间)并且仅绘制了某些化学物种。

我喜欢的是输入我感兴趣的特定样本和化学物种,并使用该输入生成一行调用gramm函数的代码 - 无需对其进行硬编码如上所示。

基本上,我需要根据输入构造一个字符串,然后将该字符串作为调用gramm的命令运行。

谢谢!

编辑:澄清

我想要一个对话框出现的东西,并要求我选择我想要的样品和化学物质。让我们说我选择样品A,B,C和化学物质Propanal和Butanal。

目前,我必须自己硬编码:

g=gramm('y', allData.(fileName).intensities, 'x', ...
    allData.(fileName).chemicalSpecies, 'color', ...
    allData.(fileName).sampleName,'subset',...
    allData.(fileName).sampleName == 'A' &... 
    allData.(fileName).chemicalSpecies =="Propanal"
    allData.(fileName).chemicalSpecies =="Butanal" | ...
    allData.(fileName).sampleName == 'B' &... 
    allData.(fileName).chemicalSpecies =="Propanal"
    allData.(fileName).chemicalSpecies =="Butanal" | ...
    allData.(fileName).sampleName == 'C' &... 
    allData.(fileName).chemicalSpecies =="Propanal"
    allData.(fileName).chemicalSpecies =="Butanal")

我不想对此进行硬编码,而是希望创建一个包含所有这些内容的字符串,然后将其作为命令传递。

我认为这可以通过使用for循环来实现,该循环将字符串连接到每个sampleSame的数组。通过这种方法,我可以创建一个字符串:

constructPlot =&#34; g = gramm(&#39; y&#39;,allData。(fileName).intensities,&#39; x&#39;,...         allData。(fileName).chemicalSpecies,&#39; color&#39;,...         。ALLDATA(文件名).sampleName,&#39;子集&#39;,...         allData。(fileName).sampleName ==&#39; A&#39; &安培; ...         allData。(fileName).chemicalSpecies ==&#34; Propanal&#34;         allData。(fileName).chemicalSpecies ==&#34; Butanal&#34; | ...         allData。(fileName).sampleName ==&#39; B&#39; &安培; ...         allData。(fileName).chemicalSpecies ==&#34; Propanal&#34;         allData。(fileName).chemicalSpecies ==&#34; Butanal&#34; | ...         allData。(fileName).sampleName ==&#39; C&#39; &安培; ...         allData。(fileName).chemicalSpecies ==&#34; Propanal&#34;         allData。(fileName).chemicalSpecies ==&#34; Butanal&#34;)&#34;

当我输入constructPlot时,它会显示字符串,但我希望它作为命令而不是字符串运行。

1 个答案:

答案 0 :(得分:2)

您可以使用ismember组合条件:

names = [...
"Butanal"  
"Butenal" 
"Pentanal" 
"Pentenal"  
"Hexanal"  
"Hexenal"  
"Heptanal"  
"Heptenal" 
"Octanal"  
"Octenal"  
"Nonanal"   
"Nonenal"  
"Butanedial"
"Pentanedial"
"Hexanedial"
"Heptanedial"
"Octanedial" 
"Nonanedial"];
condition = allData.(fileName).sampleNum < u & ...
allData.(fileName).sampleNum > l & ...  
ismemeber(allData.(fileName).chemicalSpecies ,names);