MATLAB: How to save specific variable names

namesvariable

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!

Best Answer

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)})