How can i edit a .mat file

269 ビュー (過去 30 日間)
Kim Nguyen
Kim Nguyen 2013 年 1 月 11 日
コメント済み: Steven Lord 2021 年 4 月 30 日
I have a .mat file name.mat. It contains information such as and when I open it the import wizard opens. now say the data in the import wizard is as follows:
|var_name | Ixx | Iyy | Izz
A 1 2 3
B 4 5 6
C 7 8 9
Now say I wanted to go in and update the numbers to 2-10 -- how do I do this? Or, how can I create a .mat file?
I tried using the import wizard and creating my own, but when I do, it doesn't include having the variable names in the same table because they're letters and not numbers.
Thanks

採用された回答

Image Analyst
Image Analyst 2013 年 1 月 11 日
You have to read it all in:
storedStructure = load('name.mat');
A = storedStructure.A;
B = storedStructure.B;
C = storedStructure.C;
Then change things:
A = [1 2 3];
B = rand(1,3);
C = randi(100, 3);
Then save it back:
save('name.mat', 'A', 'B', 'C');
  2 件のコメント
Arthur Vieira
Arthur Vieira 2021 年 4 月 30 日
If I have variables in my workspace that I don't want to loose (i.e. can't load the .mat in), how can I update what's in the .mat file? Including deleting a variable within a .mat file? I could load them with:
var = load('myData.mat');
But that makes 'var' a structure. And you can't save variables within structures for some reason. I'm confused on how you're meant to use .mat files.
Steven Lord
Steven Lord 2021 年 4 月 30 日
After you load the variables from the MAT-file into a struct array and modify the fields of that struct, call save with the '-struct' option and the name of the struct array.
cd(tempdir)
v = struct('x', 1:10, 'y', @sin, 'z', "abracadabra");
save('mymatfile.mat', '-struct', 'v')
whos -file mymatfile.mat
Name Size Bytes Class Attributes x 1x10 80 double y 1x1 32 function_handle z - 158 string
The MAT-file does not contain the struct v but contains three variables, one for each field in v.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 11 日
編集済み: Azzi Abdelmalek 2013 年 1 月 11 日
a=10; v=magic(4)
save filename a v
To load your file
load filename
  2 件のコメント
Kim Nguyen
Kim Nguyen 2013 年 1 月 11 日
this doesnt solve the problem of having letters, also i already have a data file, like an excel that i would like to load into matlab which is part of the problem. it seperates the character string from the number and therefore does not do what i need
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 11 日
編集済み: Azzi Abdelmalek 2013 年 1 月 11 日
fid=fopen('file1.txt')
x=textscan(fid,'%s %f %f %f')
fclose(fid)
out=cell2mat(x(2:end))
% to get the characters
d=x(1)
lett=d{:}

サインインしてコメントする。

カテゴリ

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