how to use value of a string variable in another function

10 ビュー (過去 30 日間)
Emrah
Emrah 2016 年 11 月 24 日
回答済み: Walter Roberson 2016 年 11 月 29 日
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
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.
Emrah
Emrah 2016 年 11 月 29 日
I know that @Stephen, But I couldn't find any other solution for problem. Do you have any? If you do, please share with me. Thanks.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 11 月 29 日
result = [];
for K = 1 : length(R)
result = union(result, R{K});
end

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by