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

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 Commenti

"There are 5 variables in a folder..."
A folder is an abstract concept used in computer storage which contains links to file and/or other computer directories. In contrast a variable is an array within MATLAB memory. The two are completely unrelated.
If you are actually asking about five variables in a MATLAB workspace then the simple solution is to put them into one array (e.g. a cell array) and loop over that using basic indexing. Note that numbering variables is a sign that you are doing something wrong (in this case: that your data should be in one array).
In this case it sounds like a struct array would be the best choice.
Perhaps I shouldn't have mentioned folder. This is what I mean to be more explanatory: In the current folder directory, I have crate1.mat, crate2.mat, crate3.mat, crate4.mat and crate5.mat. Opening any of these would produce 3 variables into the workspace (month, time and date). The values of these 3 variables are different for each files as listed in the current directory. So I need perform the division of 0.5 on only the variables of month and time, from crate1 to crate5. I have attached some samples for clarification.
% If I'm to do it one after the other
month = month/0.5;
time = time/0.5;
%<---- but i want to know if all could be done at a go (because this is just a sample, i really have much)--->
Opening any of these would produce 3 variables into the workspace (month, time and date)
No, your .mat file attachments all contain only one variable ('month'), except for crate1.mat
"The task needed is to divide sub-variables (only month and time) by 0.5..."
Multiplying by two is probably clearer.
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.

Accedi per commentare.

 Risposta accettata

For scalar variables,
S=dir('crate*.mat');
month = arrayfun(@(s) load(s.name).month, S)/0.5;
time = arrayfun(@(s) load(s.name).time, S)/0.5;
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 Commenti

I got this error after running the above script:
Error using arrayfun
Non-scalar in Uniform output, at index 1, output
1.
Set 'UniformOutput' to false.
Error in divide_variables (line 12)
month = arrayfun(@(s) load(s.name).month, S)/0.5;
Move the division to inside the arrayfun call and do what the error message tells you.
This means one or more of the month fields is not scalar.
Still got error messages after moving the division to inside the arrayfun call.
Variable 'month' is a 1000x1 double while 'time' is 1x1 double. I've attached samples of how it looks like in previous comment. In the code presented, I even comment out 'month' to see if the last line of the code 'time' would work, but also displayed error of reference to non-existent field 'time'. I guess I'd have to start doing this task manually since the task seems not feasible. Thank you all for your attention.
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;
I want to ask this for future purpose, what if the .mat files are having completely different names instead of been indexed? For instance, instead of crate1, crate2, crate3, crate4, crate5, we rather have blue, yellow, white, purple and green. By that, how'd I edit the first line of the presented code?

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by