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

3 ビュー (過去 30 日間)
blues
blues 2019 年 10 月 22 日
編集済み: Deepak 2025 年 1 月 16 日
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 Center および File ExchangeWorkspace 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