How can I delete a particular element in a set of workspace variables?
    5 views (last 30 days)
  
       Show older comments
    
My workspace has a set of variables with a common prefix. Most of the variables are sized 839 X 1 single. But some of these variables are 840 X 1 single. The last element in the 840 X 1 variable has to be deleted. My aim is to use a for loop to go through all the variables in the workspace with the prefix and then delete the last element in variables where there are more than 839 elements. 
So far, I used whos to create a structural aray with the details of the variables with the appropriate prefix:
C = whos ('CSUS114_dFF_*')
I then used a for loop to create an array with the names of the variables which need their last element to be deleted:
for r = 1:length(C)
    if C(r).size(1)>839
        a{r} = C(r).name
    end
end
a(cellfun('isempty',a))=[];
This gave me the right result with the names of the variables. However, I am not sure how to proceed to the next step which would be to somehow use this array to delete the last element of the respective variables. Any ideas?
3 Comments
  Deepak Gupta
      
 on 26 May 2020
				I think better would be you reassign your variables with required size. For exp:
x = randn(840, 1); %Variable with size 840*1
x = x(1:839); % Here variable will be truncated to 839*1 if it's more than this size.
Answers (1)
  Fangjun Jiang
      
      
 on 26 May 2020
        
      Edited: Fangjun Jiang
      
      
 on 26 May 2020
  
      For a one-time processing, you can do this
for r = 1:length(C)
    if C(r).size(1)>839
        str=sprintf('%s(840:end)=[];',C(r).name);
        evalin('base',str);
    end
end
0 Comments
See Also
Categories
				Find more on Loops and Conditional Statements in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


