subsref works as not expected
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Sergey Kasyanov
      
 el 12 de En. de 2021
  
    
    
    
    
    Comentada: Sergey Kasyanov
      
 el 12 de En. de 2021
            Hello!
Let's create random cell array.
test = {1,2,3,4};
And try to evaluate simple code
res_1 = ones(subsref(test, substruct('{}', {':',':'})));
res_2 = ones(test{:, :});
Is there any way to unpack cell array without direct access through {:}?
0 comentarios
Respuesta aceptada
  Bruno Luong
      
      
 el 12 de En. de 2021
        
      Editada: Bruno Luong
      
      
 el 12 de En. de 2021
  
      I knew it for sometime: subsref never work to emulate comma list (not sure if it's documented), because a non trivial comma list (with length > 1) can never be considered as function output .
This limitation surely deserves to be in "what frustrates you ..." thread.
3 comentarios
  Bruno Luong
      
      
 el 12 de En. de 2021
				
      Editada: Bruno Luong
      
      
 el 12 de En. de 2021
  
			First MATLAB won't allow such cascade syntax (this deserves to be  "what frustrates you ..." thread as well)
some_function(var_1, var_2){:}
Example:
>> num2cell(1:3)
ans =
  1×3 cell array
    {[1]}    {[2]}    {[3]}
>> num2cell(1:3){:}
Error: Indexing with parentheses '()' must appear as the last operation of a valid indexing expression.
The best you can do is this
A = rand(3,4,5)
idx = some_function(1:2,3:4,':');
B = A(idx{:})
function c = some_function (varargin)
c = varargin;
end
But then the function some_function is not useful in any way. Just work directly with cell and comma list will do the same job:
A = rand(3,4,5)
idx = {1:2,3:4,':'};
B = A(idx{:})
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

