How to load all variables from *.mat except one?

25 ビュー (過去 30 日間)
Franziska
Franziska 2013 年 8 月 7 日
編集済み: Toby 2017 年 10 月 12 日
Hi all,
I want to load all variables from a *.mat-file except one. Here are my lines:
X = 10;
if restart == 1
clearvars -except zz X ds_restart delta_t
load(['restart_safety_' num2str(X)], '-regexp', '^(?!delta_t)...');
restart = 0;
end
As far as I understood, this will load all variables from restart_safety_10.mat and write over except those having 'delta_t' in their name. I want to keep 'delta_t' from before and do not want to load it. Unfortunatly, there are further variables ignored named e.g. 'N'. I am sure all variables are in the .mat-file since I can load all variables using only
load(['restart_safety_' num2str(X)])
What is happening here? Why are there more variables ignored? Does anyone know a better way than working with regular expression?
Thanks for your help. Franziska

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 7 日
Use
load(['restart_safety_' num2str(X)], '-regexp', '^(?!delta_t)\w');
  1 件のコメント
Franziska
Franziska 2013 年 8 月 7 日
Thanks a lot! It works.

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

その他の回答 (1 件)

Toby
Toby 2017 年 9 月 27 日
編集済み: Toby 2017 年 10 月 12 日
As another example, you can load all variables except those which already existed in the workspace:
varlist = who; %Find the variables that already exist
varlist =strjoin(varlist','$|'); %Join into string, separating vars by '|'
load(TSfile,'-regexp', ['^(?!' varlist ')\w']);

カテゴリ

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