how to use value of a string variable in another function
9 次查看(过去 30 天)
显示 更早的评论
Hi. I am trying to use union() to find union of some arrays which are belong to a cell. And I want to that in a loop. Here is what I have done so far (actually nothing), but still:
str='';
for i=1:G.NodeCount
str=strcat(str,'cell2mat(R{',num2str(i),',1}(:,1)),');
end
str = str(1:end-1); %delete the last comma
The result of str is exactly the what I want, but I am not going to put it here due to the length. However I'd like to show first three and the last one (comma seperated):
cell2mat(R{1,1}(:,1)),cell2mat(R{2,1}(:,1)),cell2mat(R{3,1}(:,1))...cell2mat(R{129,1}(:,1))
Finally, I want to use the value of 'str' in the union() function. None of them below are worked:
result=union(disp(sprintf('%s\n',str)));
result=union(disp(str));
result=union(sprintf(str));
result=union(sprintf('%s\n',str));
result=union(fprintf(str));
result=union(fprintf('%s\n',str));
What can I do else? Thanks in advance.
3 个评论
Stephen23
2016-11-24
@Emrah: you seem to be constructing code as strings, which presumably you will try to execute. This is a very buggy, slow, and unclear way to write code. Executing strings will make your own life much harder. You might like to consider changing to use more efficient coding paradigms.
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!