how to use value of a string variable in another function
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Stephen23
il 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.
Risposte (1)
Walter Roberson
il 29 Nov 2016
result = [];
for K = 1 : length(R)
result = union(result, R{K});
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!