Reading a file and loading variables using function
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
dpb
2023-3-5
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
S=load('sample.mat')
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'})
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'})
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!