MATLAB: How can I use regular expressions to collect a list of variable names that satisfy a particular pattern?
-
I have a set of variables in my workspace. I want to use regular expressions regexp to get the name of the variable names that match a particular pattern.
-
Answer:
Step 1: Get all the MATLAB variable names from the workspace using listOfVarNames = whos; . Step 2: Iterate over all variable and check if they match the pattern of your interest: for idx = 1:numel(listOfVarNames); regexp(listOfVarNames,'pattern'); end The code above will print if the variable name matched the pattern or not.
Lokesh Amarnath at Quora Visit the source
Other answers
Building on another answer: % Gets variables from current workspace. listOfVarNames = whos; % Use strfind to do the regexp stringsWhichMatch = strfind( {listOfVarNames.name}, 'pattern' ); % Use find to get the indices of interest indicesOfVariables = find( [ stringsWhichMatch{:} ] ); % Dump variables names into a cell array variablesOfInterest = { listOfVarNames( indicesOfVariables ).name} The documentation is your friend: http://www.mathworks.com/support/ Look for the functions that you need more info on in the documentation.
Prabhakar Govind
Related Q & A:
- How can I remove an element in a repeated list?Best solution by Stack Overflow
- How can I use a button to retrieve a phone 'number' from contacts?Best solution by Stack Overflow
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I get my regular avatar of a real picture back as my avatar instead of the one now?Best solution by Yahoo! Answers
- How can I use my digital camera as a webcam?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.