How to write a .mat file with saving same variable with different values without overwriting previous values?

7 次查看(过去 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.

回答(1 个)

Deepak
Deepak 2025-1-16
编辑:Deepak 2025-1-16
Hi @blues,
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:
I hope this helps in resolving the issue.

类别

Help CenterFile 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!

Translated by