# How to perform arithmetic operation that would affect all variables in a folder at once

2 vues (au cours des 30 derniers jours)
Ayobami Meadows le 28 Déc 2021
There are 5 variables in a folder (crate1, crate2, crate3, crate4, crate5). Each of these variables have 3 sub-variables inside them. The 3 sub-variables have same names but different values, for all the variables. For example, crate1 has month, time, date. Crate2 also has month, time, date. And so on up to crate5... The task needed is to divide sub-variables (only month and time) by 0.5, for all the variables crate1 to crate5... Instead of doing it manually one after other, is there a way I could do for all at same time?
##### 7 commentairesAfficher 5 commentaires plus anciensMasquer 5 commentaires plus anciens
Stephen23 le 29 Déc 2021
"The task needed is to divide sub-variables (only month and time) by 0.5..."
Multiplying by two is probably clearer.
Ayobami Meadows le 29 Déc 2021
Truly correct. But also understand that these are not the actual values of the task. I just create these samples to clearly demonstrate what I need to fulfill the main task with the original values.

Connectez-vous pour commenter.

### Réponse acceptée

Matt J le 28 Déc 2021
Modifié(e) : Matt J le 28 Déc 2021
For scalar variables,
S=dir('crate*.mat');
EDIT: For non-scalar variables,
S=dir('crate*.mat');
month = cell2mat( arrayfun(@(s) load(s.name).month, S','uni',0) )/0.5;
time = cell2mat(arrayfun(@(s) load(s.name).time, S','uni',0) )/0.5;
##### 8 commentairesAfficher 6 commentaires plus anciensMasquer 6 commentaires plus anciens
Walter Roberson le 1 Jan 2022
S = dir('*.mat');
Ayobami Meadows le 2 Jan 2022

Thank you.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Dynamic System Models dans Help Center et File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by