how to use value of a string variable in another function

9 visualizaciones (últimos 30 días)
Emrah
Emrah el 24 de Nov. de 2016
Respondida: Walter Roberson el 29 de Nov. de 2016
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 comentarios
Stephen23
Stephen23 el 24 de Nov. de 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.
Emrah
Emrah el 29 de Nov. de 2016
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.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 29 de Nov. de 2016
result = [];
for K = 1 : length(R)
result = union(result, R{K});
end

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by