How to save specific variable names

Hello!
I have a large workspace and i want to save workspace variables that includes specific characters and letters. For example i want to save multiple variables that contains "T22P50" in their name. For instance, I got the variable names "Pressure_T22P50", "Position_T22P50" etc and i want to save these into a .mat file. Since these variables changes for different tests (T22P50, T30P50 etc), is there a way that the save function can search in the workspace for variables containing the specific characters, instead of manually changing them each time?
I hope the question is clear, thanks in advance!

1 件のコメント

Stephen23
Stephen23 2017 年 4 月 27 日
編集済み: Stephen23 2017 年 4 月 27 日
Note that your code would be much simpler, faster, and more reliable if you did not put meta-data into variable names:
Better code (simpler, faster, more reliable, easier to read,...) would simply use indexing or fieldnames instead of putting meta-data into variable names. Then your question would simply require saving one variable, and would not rely on ugly hack code.

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

 採用された回答

KL
KL 2017 年 4 月 27 日
編集済み: KL 2017 年 4 月 27 日

0 投票

workspaceVars = who;
findVars = strfind(workspaceVars, 'P250');
indexVars = find(not(cellfun('isempty', findVars)));
Now you can use workspaceVars and indexVars along with save. For example,
save('name.mat',workspaceVars{indexVars(1)})

5 件のコメント

abj9
abj9 2017 年 4 月 27 日
That is perfect, thank you!
I have an additional question. Is it possible to change whats inside the apostrophes (' ') several places at once (like shift+enter)? Now I have to change it manually each place.
Best regards!
KL
KL 2017 年 4 月 27 日
What do you mean? In the above code, 'P250' and 'isempty' both are between quotes but I don't think you'd wanna change 'isempty'.
abj9
abj9 2017 年 4 月 27 日
編集済み: abj9 2017 年 4 月 27 日
I mean like in general. If i want to change "T22P50" several places in the script at once, when it is inside apostrophes.
For instance: addpath('.....\T22P50')
fopen('T22P50')
save('T22P50')
Is it possible to change whats inside the ' ' term just once, and the others will change accordingly, or do I have to change it manually each place?
KL
KL 2017 年 4 月 27 日
Intuitive idea is to define that as a variable and just change it just once at the top. If you have all the possible names in an array, even better.
abj9
abj9 2017 年 4 月 27 日
Thank you, KL! Your answers helped me a lot :)

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2017 年 4 月 27 日

0 投票

Vars=who('*_T22P50');
save('MatFile',Vars{:});

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

タグ

質問済み:

2017 年 4 月 27 日

コメント済み:

2017 年 4 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by