How can I delete a particular element in a set of workspace variables?

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
Deepak Gupta
Deepak Gupta il 26 Mag 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.
Aaron S
Aaron S il 26 Mag 2020
Thank you for these suggestions.
Stephen, I agree - I wanted to avoid using the eval family of functions - which is why I even asked the question.
Deepak, I think your suggestion is a good one - better to truncate earlier as you have pointed out.

Accedi per commentare.

Risposte (1)

Fangjun Jiang
Fangjun Jiang il 26 Mag 2020
Modificato: Fangjun Jiang il 26 Mag 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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by