How to write a .mat file with saving same variable with different values without overwriting previous values?
7 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Deepak
on 16 Jan 2025
Edited: Deepak
on 16 Jan 2025
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 Comments
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!