How to call all arrays that start with the name "CV_"?

4 visualizaciones (últimos 30 días)
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 2 de Oct. de 2019
Editada: Stephen23 el 3 de Oct. de 2019
I need to check a value in all arrays that their names start with "CV_". File names are like CV_er, CV_wer, CV_jkl and so on. Each of these are arrays that have multiple rows and column. I am interested to find if any member of these arrays has the number larger than 1 in them. I need to report the number and the index (whihc array).
Any idea?

Respuestas (2)

James Tursa
James Tursa el 2 de Oct. de 2019
Don't do that. See this post for reasons why:
Other methods (e.g., cell arrays) are better for this.
  3 comentarios
Walter Roberson
Walter Roberson el 2 de Oct. de 2019
No, you would not have that question: you would have to question of how to build all of those into the same variable instead of using separate variables.
Stephen23
Stephen23 el 3 de Oct. de 2019
Editada: Stephen23 el 3 de Oct. de 2019
" Even if i work with cell arrays I would have the same question!"
Not really: accessing the contents of one cell array is simple and efficient (e.g. using indexing or functions like cell2mat, etc.), unlike what you are trying to do.
'How those starting with "CV_"'
That is exactly what you should avoid doing. Accessing variable names dynamically is one way that beginners force themselves into writing slow, complex, obfuscated, buggy, hard-to-debug code.
James Tursa already gave you one much simpler and more efficient alternative (cell arrays).

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 2 de Oct. de 2019
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
if any( reshape(eval(CV_vars{K}), [], 1)>1 )
vars_gt_1{end+1} = CV_vars{K};
end
end
This is not recommended. You will run into problems using this kind of code, and the problems will be hard to debug, and the code will mysteriously be slow.
  2 comentarios
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 2 de Oct. de 2019
@ Walter Roberson what does reshape(eval(CV_vars{K}), [], 1) do? and this code doesnot show the row and column of the array that with value larger than 1.
Walter Roberson
Walter Roberson el 2 de Oct. de 2019
CV_vars is a cell array of character vectors. CV_vars{K} is one particular character vector. eval() of the character vector returns the content of the variable named by the character vector.
We want to test if there is at least one entry in the variable that is > 1. We cannot assume that the variable is a scalar or vector or even that it is 2D. If it were scalar or vector we could use any(VALUE>1) and if it were scalar or vector or 2D we could use any(any(VALUE>1)), but since we do not know how many dimensions it is, we either have to test the number of dimensions and have a separate test for each, or else we have to reshape the results from however many dimensions it is into a vector so that we can apply just a single any() to it.
An alternate coding would be:
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
this_var = eval(CV_vars{K});
if nnz( this_var > 1 ) > 0
vars_gt_1{end+1} = CV_vars{K};
... and do something here to report the rows and columns
end
end
A third coding would be:
CV_vars = who('CV_*');
vars_gt_1 = {};
for K = 1 : length(CV_vars)
this_var = eval(CV_vars{K});
loc_linear_idx = find(this_var>1);
if ~isempty(loc_linear_idx)
vars_gt_1{end+1} = CV_vars{K};
... and do something here to report the rows and columns given the linear indices
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by