printing the name of the data containing a certain value in the workspace
3 views (last 30 days)
Show older comments
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
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
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)
Now you can easily use the inbuilt tools for processing your data, e.g.:
format compact
summary(T)
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
Accepted Answer
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));
2 Comments
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!