How to load data without reloading?

I would like to load variables from a .mat file, but just those ones which are not loaded before. It is possible not overwrite the variables?

답변 (1개)

Chad Greene
Chad Greene 2015년 10월 27일

0 개 추천

As I understand it, you have some variables in your workspace, let's call them x and y. You want to load some test.mat file that has variables x and y in them. You can do
x = 1;
y = 2;
t = load('test.mat');
x2 = t.x;
y2 = t.y;

댓글 수: 3

Mr M.
Mr M. 2015년 10월 27일
My problem is that I dont know I have x or not. Maybe yes maybe no. If I have x than I would like to load x, however in case of existed x I dont want to load x. And I want to do it automatically for all variables in the .mat file without knowing the names
You can try
m = matfile('test.mat');
w = who(m);
to the get names of the variables in test.mat.
Or, putting it all together, if test.mat contains a variable named x, load it:
if any(strcmpi(who(matfile('test.mat')),'x'))
x = load('test.mat','x');
end
Similarly, you could compare the variables in test.mat to the variables in your workspace using ismember with who.

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2015년 10월 27일

댓글:

2015년 10월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by