how to use value of a string variable in another function
9 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 24 Nov 2016
@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.
Answers (1)
Walter Roberson
on 29 Nov 2016
result = [];
for K = 1 : length(R)
result = union(result, R{K});
end
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!