Reading a file and loading variables using function

2 visualizaciones (últimos 30 días)
MattC
MattC el 5 de Mzo. de 2023
Respondida: dpb el 5 de Mzo. de 2023
Hi All, this is how my data looks like is there a way we can put these steps in a for loop or a function?
%Each .mat file looks like
A = 10;
B = 30;
C =15;
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
%What I am interested in creating:
Day{1} = [10; %A
30; %B
15];%C
Day{2} = [20; %A
40; %B
10];%C
Day1Thrshld = [A_Threshold; B_Threshold; C_Threshold];
Day2Thrshld = [A_Threshold; B_Threshold; C_Threshold];
For me this data comes from a .mat file for each day. So each .mat file data will have all these variables as shown above
Day1.mat --> A,B,C and A_threshold, B_threshold, C_threshold
Day2.mat --> A,B,C and A_threshold, B_threshold, C_threshold
Day3..., Day4....
And I interested in appending/creating this Day{1},Day{2},Day{3},... variables with (A,B,C) values by reading the .mat files
The challenge for me in creating the function/for loop for above requirement is. The working directory is set but I am not sure how to pull a .mat file and then fetch these variables from it using a function/loop or the right approach.
I do know for a fact that timestamps would be used for different .mat files because let's day Day1.mat was created on 3/4/2023 and Day2.mat was created on 3/5/2023. However if the Day1 file (or any history file) is it is already used/fetched we do not want to use that/fetch data from.

Respuestas (1)

dpb
dpb el 5 de Mzo. de 2023
A = 10;
B = 30;
C =15;
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
save sample.mat A B C A_Threshold B_Threshold C_Threshold
whos -file sample
Name Size Bytes Class Attributes A 1x1 8 double A_Threshold 1x1 8 double B 1x1 8 double B_Threshold 1x1 8 double C 1x1 8 double C_Threshold 1x1 8 double
S=load('sample.mat')
S = struct with fields:
A: 10 A_Threshold: 70 B: 30 B_Threshold: 80 C: 15 C_Threshold: 85
tSample=table([S.A;S.B;S.C],[S.A_Threshold;S.B_Threshold;S.C_Threshold],'VariableNames',{'Data','Threshold'});
day=ones(height(tSample),1);
tSample=addvars(tSample,day,'Before','Data','NewVariableNames',{'Day'})
tSample = 3×3 table
Day Data Threshold ___ ____ _________ 1 10 70 1 30 80 1 15 85
If the files all do use the same variable name for each, then you can just code them directly; and each is located positionally for each Day in the table. That may not be quite convenient-enough, you can also get the field names and use them as another variable for grouping or selection...
vars=fieldnames(S); vars=categorical(vars(1:2:end));
tSample=addvars(tSample,vars,'Before','Data','NewVariableNames',{'Unit'})
tSample = 3×4 table
Day Unit Data Threshold ___ ____ ____ _________ 1 A 10 70 1 B 30 80 1 C 15 85
When you get the format for the timestamp, you can then parse it from the filename, converting it to a <datetime> variable, then test that it is/is not already included in the table before importing it.
This avoids the dreaded issue of having variable names that contain metadata like the UnitID or somesuch that make it extremely hard to write generic code without the use of the ugly eval technique.
You'll have to make real variable names for what it is these data represent; I just created a new data storage scheme that will be much simpler to use, making up names for the undefined...

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by