2016-11-28 3 views
0

에 matlab에 속성을 저장 : 나는 .mat 파일에 구조체를 저장하려는내가 <code>myStruct</code>라는 <code>struct</code> 속성 클래스가 매트 파일

properties 
    myStruct; 
end 

합니다. 나는 시도 :

save -append registers.mat myStruct; 

그러나 오류 제공합니다

Variable 'myStruct' not found. 

내가처럼 struct 업데이트를위한 다양한 기능을 가지고 :

function configureChannel(obj, Channel) 
     myStruct.Channel = Channel; 
     save -append registers.mat myStruct; 
    end 

    function updateConfiguration(obj, dataPath) 
     myStruct.EN = 1; 
     save -append registers.mat myStruct; 
    end 

이이 같은 클래스의 모든 기능됩니다.

+0

클래스 속성 앞에 클래스 속성을 추가해야합니다. – excaza

+0

이런 뜻이야? save -append registers.mat myClass.myStruct 작동하지 않습니다. – user1876942

+0

'save -append registers.mat obj.myStruct;를 시도하십시오. 작동하지 않으면 임시 변수에 저장 한 다음 변수를 저장하십시오 ('a = obj.myStruct; save ...'). 또한,'myStruct.Channel = Channel;을 할 때 새로운 지역 변수를 생성한다고 확신합니다. 대신에'obj.myStruct.Channel = Channel;'을 실행해야합니다. –

답변

2

이 코드의 주된 문제는 사용자의 기능에서 myStruct에 액세스하는 방법입니다. 다음 코드 조각을 살펴 보자 :

function configureChannel(obj, Channel) 
    myStruct.Channel = Channel; 
    ... 
end 

당신이 이 그것을 할 수 있도록을 목적으로 현재 객체의 myStructChannel 속성의 Channel 필드에 Channel를 할당하는 것이 었습니다 무엇. 무엇 실제로하는 일은 호출하는 것과 같습니다이다

myStruct = struct('Channel', Channel); 

, 당신은 실제로 객체의 속성을을 업데이트하지 않고 configureChannel, 의 현재 작업 공간/범위에 새로운 지역 변수를 작성, obj .

obj.myStruct.Channel = Channel; 

다음 절약의 문제 : 당신이 당신의 settermyStruct에 액세스하는 방법

그래서 당신이해야 할 첫 번째 보정입니다.

파일에 객체의 필드의 사본을 덤핑 할 때, 나는 struct로 임의의 객체를 치료하기 위해 save을 기대 등 struct saving conventions 적용한다 : 따라서

'-struct',structName | Store the fields of the scalar structure specified by structName as individual variables in the file. For example, save('filename.mat','-struct','S') saves the scalar structure, S .

, 나는 다음을 기대 면책 조항을 작동 : 나는 그것을를 테스트하지 않았다

save('filename.mat','-struct','obj.myStruct'); 

다른 대안을 경우에 위의 실패

save('filename.mat','obj.myStruct');   % Alternative #1 
tmp = obj.myStruct; save('filename.mat','tmp'); % Alternative #2 

마지막 한가지 - 당신이 눈치 챘을 수도로 나는 그것을 읽을 수/더 유지 보수로 간주하기 때문에, 내가 save의 기능 양식을 사용하고/바보 - 증거 :

save(filename,variables,'-append'); 
관련 문제