How to store a matrix so that I can call upon it in any other file I am working in?
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Scott Banks
 el 24 de Jul. de 2025
  
    
    
    
    
    Comentada: Scott Banks
 el 25 de Jul. de 2025
            Hi there,
I am trying to save a matrix of data that I want to use, so that I can call upon it whenever I wish. Plus, I don't want it taking up space in my other script I am working on. I have been searching online how to do this apparent simple task, but can't seem to get it right.
So say I'm in a file called Frame2.mat, and the matrix is:
A = rand(10,10)
Then I go to another file I am working in and want to call upon matrix A and extract the first column.
So in this different file (not Frame2) I want to use:
T = A(:,1)
How do I do this?
Many thanks
Scott
1 comentario
  Stephen23
      
      
 el 25 de Jul. de 2025
				"So say I'm in a file called Frame2.mat, and the matrix is: A = rand(10,10)"
Are you attempting to write a .mat file yourself as a text file? That does not make sense.
Respuesta aceptada
  Torsten
      
      
 el 24 de Jul. de 2025
        
      Editada: Torsten
      
      
 el 24 de Jul. de 2025
  
      Declare the matrix as "global" or pass it as input argument to all functions where it is needed.
Or save it as a .mat-file and load it where it is needed.
8 comentarios
  Walter Roberson
      
      
 el 24 de Jul. de 2025
				Do not save your variable as a .m file, save it as a .mat file.
Or if you really want, save it as a .csv or .txt file that you then readmatrix at need, if you need the file to be editable.
You could also consider
function A = loadA()
  persistent A
  if isempty(A)
      A = load('Matrix.mat', 'A').A;
  end
end
after which you would call
A = loadA();
This had the advantage of only reading in A once and remembering the cached value the second and later times.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Whos 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!





