printing the name of the data containing a certain value in the workspace

1 view (last 30 days)
Hello, let's assume we have 5 data in the workspace.
a=10
b=20
c=30
d=40
reference=20
What I want to do is to take the difference of each value with the reference and determine the largest data from the reference.
So for example;
x1= a-ref
x2= b-ref
x3= c-ref
x4= d-ref
max_delta= max(x1,x2,x3,x4)
Winner = "d" --> I want a result like this. What formula should I use for this?
Thank you very much.
  6 Comments
Stephen23
Stephen23 on 14 Nov 2023
Edited: Stephen23 on 14 Nov 2023
"My files are in parquet format and unfortunately, although the naming is similar, each file has a different name. The numbers in the names change regularly."
The filenames are irrelevant. Read my previous comment: I was talking about variable names, not filenames.
Somehow you must have magically created lots of variables from those parquet files. Best avoided.
unzip demo.zip
dir
. CHR_Heating_0_Cooling_40.parquet CHR_Heating_10_Cooling_40.parquet CHR_Heating_20_Cooling_40.parquet CHR_Heating_neg10_Cooling_40.parquet .. CHR_Heating_0_Cooling_45.parquet CHR_Heating_10_Cooling_45.parquet CHR_Heating_20_Cooling_45.parquet CHR_Heating_neg10_Cooling_45.parquet CHR_Heating_0_Cooling_30.parquet CHR_Heating_10_Cooling_30.parquet CHR_Heating_20_Cooling_30.parquet CHR_Heating_neg10_Cooling_30.parquet demo.zip CHR_Heating_0_Cooling_35.parquet CHR_Heating_10_Cooling_35.parquet CHR_Heating_20_Cooling_35.parquet CHR_Heating_neg10_Cooling_35.parquet
P = '.'; % absolute or relative path to where the files are saved
S = dir(fullfile(P,'CHR*.parquet'));
for k = 1:numel(S)
% Import file data:
F = fullfile(S(k).folder,S(k).name);
T = parquetread(F);
% Extract heating and cooling:
X = regexp(S(k).name,'(pos|neg)?\d+','match');
V = str2double(regexprep(X,'neg','-'));
T{:,'Heating'} = V(1);
T{:,'Cooling'} = V(2);
S(k).data = T;
end
% Concatenate all tables together to make the data much easier to work with:
T = vertcat(S.data)
T = 92438×13 table
SOC Tmax Tmin Current Voltage StationLoad ChrUsableEng TotalLossEng LossDCREng Duration TAvg Heating Cooling ______ _______ _______ _______ _______ ___________ ____________ ____________ __________ ________ _______ _______ _______ 20 -20 -20 0 377.97 0 0 0 0 0 -20 0 30 20.004 -19.913 -19.999 32.822 384.88 0.005224 0.0032292 0.0019948 5.0371e-05 1 -19.999 0 30 20.008 -19.77 -19.998 32.824 384.94 0.010678 0.0066754 0.0040025 0.00011358 2 -19.998 0 30 20.012 -19.62 -19.998 32.826 384.99 0.016132 0.010122 0.0060106 0.00017727 3 -19.998 0 30 20.016 -19.47 -19.997 32.828 385.04 0.021588 0.013568 0.0080192 0.00024139 4 -19.997 0 30 20.02 -19.32 -19.996 32.83 385.09 0.027044 0.017015 0.010028 0.00030592 5 -19.996 0 30 20.025 -19.17 -19.995 32.832 385.13 0.0325 0.020463 0.012038 0.00037084 6 -19.995 0 30 20.029 -19.022 -19.994 32.834 385.18 0.037957 0.02391 0.014047 0.00043611 7 -19.994 0 30 20.033 -18.873 -19.993 32.837 385.22 0.043415 0.027358 0.016057 0.00050173 8 -19.993 0 30 20.037 -18.726 -19.993 32.839 385.25 0.048874 0.030806 0.018068 0.00056766 9 -19.992 0 30 20.041 -18.579 -19.992 32.841 385.29 0.054332 0.034254 0.020078 0.0006339 10 -19.991 0 30 20.045 -18.432 -19.991 32.843 385.33 0.059792 0.037703 0.022089 0.00070043 11 -19.99 0 30 20.049 -18.286 -19.99 32.845 385.36 0.065252 0.041151 0.024101 0.00076723 12 -19.989 0 30 20.054 -18.141 -19.989 32.847 385.39 0.070713 0.044601 0.026112 0.00083431 13 -19.988 0 30 20.058 -17.996 -19.989 32.849 385.42 0.076174 0.04805 0.028124 0.00090163 14 -19.987 0 30 20.062 -17.851 -19.988 32.851 385.45 0.081635 0.051499 0.030136 0.0009692 15 -19.986 0 30
Now you can easily use the inbuilt tools for processing your data, e.g.:
format compact
summary(T)
Variables: SOC: 92438×1 single Values: Min 20 Median 46.592 Max 80.023 Tmax: 92438×1 single Values: Min -20 Median 15.907 Max 34.224 Tmin: 92438×1 single Values: Min -20 Median -3.2488 Max 14.606 Current: 92438×1 single Values: Min 0 Median 85.415 Max 133.49 Voltage: 92438×1 single Values: Min 377.97 Median 411.5 Max 442.54 StationLoad: 92438×1 single Values: Min 0 Median 28.142 Max 64.612 ChrUsableEng: 92438×1 single Values: Min 0 Median 22.694 Max 53.152 TotalLossEng: 92438×1 single Values: Min 0 Median 4.44 Max 11.468 LossDCREng: 92438×1 single Values: Min 0 Median 0.75362 Max 1.7878 Duration: 92438×1 single Values: Min 0 Median 2888 Max 7692 TAvg: 92438×1 single Values: Min -20 Median 10.426 Max 31.779 Heating: 92438×1 double Values: Min -10 Median 0 Max 20 Cooling: 92438×1 double Values: Min 30 Median 40 Max 45
If you really want to process the data from just one file then you can do that too, either by filtering the table T or by accessing the data in structure S, e.g. for the 2nd file:
S(2).name % filename
S(2).data % filedata
yunus ay
yunus ay on 14 Nov 2023
This for loop is definitely amazing for me. Solved my many others problem too.
For example, I will compare my Station Load column in each of my 896 files with each other.
I can do this more practically with the S file structure. Thank you very much

Sign in to comment.

Accepted Answer

Les Beckham
Les Beckham on 13 Nov 2023
a=10;
b=20;
c=30;
d=40;
reference=20;
names = ["a", "b", "c", "d"];
[max_delta, idx] = max([a b c d] - reference);
fprintf('The largest difference from the reference is %d, for variable %s\n', max_delta, names(idx));
The largest difference from the reference is 20, for variable d
  2 Comments
Les Beckham
Les Beckham on 13 Nov 2023
You are quite welcome.
Stephen23 makes a good point. It is generally easier to operate on data in vectors and/or matrices than in a bunch of different variables. Note that I created a vector out of your separate variables in order to find the max delta.
If this answer answers your question, please Accept it. Thanks.

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!