필터 지우기
필터 지우기

How to change the value of a double variable in a .mat file

조회 수: 2 (최근 30일)
ericson
ericson 2014년 3월 26일
편집: Joseph Cheng 2014년 3월 26일
Hi! In have a mat file which has 3 main variables a1, a2, and a3. At the time, each of the 3 has 10 variables in it from b1 to b10
example:
a1.b1, a1.b2, ..... , a1.b10
a2.b1, a2.b2, ..... , a2.b10
a3.b1, a3.b2, ..... , a3.b10
I want to change the value of a1.b1 from 0 to 1
but the problem is that I can't use the code
save('sample.mat','a1.b1','-append')
I can't use save('sample.mat','a1','-append') because the variables a1.b2 to a1.b10 will be lost. Is there a code which can make me do this kind of thing?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 3월 26일
편집: Azzi Abdelmalek 2014년 3월 26일
If your file is not big, just load your data, modify them, then save again your data
%Look at this example
a.b1=1;
a.b2=2;
c.b1=1;
c.b2=20;
save fic a c
clear
data=load('fic','a')
a=data.a;
a.b1=0;
save('fic','a','-append')
  댓글 수: 3
ericson
ericson 2014년 3월 26일
I tried this but it, based on your code. a.b2 will be lost after doing that
Azzi Abdelmalek
Azzi Abdelmalek 2014년 3월 26일
No, if you load the variable a, a.b2 will not be lost

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2014년 3월 26일
편집: Joseph Cheng 2014년 3월 26일
From the documentation '-append' adds a new variable to the mat file, so I do not think it will append to an existing struct.
Have you tried save('sample.mat','a1','-append') and rewrite all a1? by doing something like this?
load('sample.mat','a1'); %load in just a1 variable
a1.b1=1;
save('sample.mat','a1','-append')
  댓글 수: 3
ericson
ericson 2014년 3월 26일
When I experimented on it, when there is an existing variable which is the same as the one that I'm going to append, it will overwrite it.
I tried that and it deleted the other variables stored in a1
Joseph Cheng
Joseph Cheng 2014년 3월 26일
편집: Joseph Cheng 2014년 3월 26일
Yes so i suggest loading just a1, swap a1.b1 and save by appending a1. You will not be able to adjust a1.b1 without loading in a1.

댓글을 달려면 로그인하십시오.

카테고리

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