printing the name of the data containing a certain value in the workspace
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Stephen23
2023년 11월 14일
편집: Stephen23
2023년 11월 14일
"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
채택된 답변
Les Beckham
2023년 11월 13일
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
Les Beckham
2023년 11월 13일
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.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!