How to write a .mat file with saving same variable with different values without overwriting previous values?
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
Hi, 
I have 4 variables in the following format: (I, J, K) = Value. I would like to write a matfile such that I can have
(0, 0, 0) = 0.1;
(0, 0,1 ) = 0.2; 
(0, 1, 0) = 0.3; 
......
How can I write this kind of .mat file? Any help would be hugely appreciated.
댓글 수: 0
답변 (1개)
  Deepak
 2025년 1월 16일
        
      편집: Deepak
 2025년 1월 16일
  
      We can achieve the desired .mat file structure by first defining the maximum dimensions for the indices (I, J, K) and initializing a 3D matrix of zeros to accommodate all potential index combinations. By assigning values to specific positions in this matrix, where each position corresponds to a unique (I, J, K) coordinate, we effectively map each coordinate triplet to its respective value. Finally, use "save" function in MATLAB to store the matrix in a .mat file, ensuring the data is organized and can be easily accessed or modified later.
Below is the sample MATLAB code for the same:
% Define the maximum dimensions for I, J, K
maxI = 1; % maximum index for I
maxJ = 1; % maximum index for J
maxK = 1; % maximum index for K
% Initialize a 3D matrix with zeros
data = zeros(maxI + 1, maxJ + 1, maxK + 1);
% Assign values to the matrix at specific indices
data(1, 1, 1) = 0.1; % (0, 0, 0) = 0.1
data(1, 1, 2) = 0.2; % (0, 0, 1) = 0.2
data(1, 2, 1) = 0.3; % (0, 1, 0) = 0.3
% Save the matrix to a .mat file
save('myData.mat', 'data');
Please find attached the documentation of functions used for reference:
Array Indexing: www.mathworks.com/help/matlab/math/array-indexing.html
I hope this helps in resolving the issue.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!