How to get specific points for certain value from multiple excel files and insert them into the figure as legend?

4 views (last 30 days)
Hello,
i have two questions that are somewhat related.
1) i have mutliple excel files that contains columns of data;
example (inserting in the code, as not sure how to put the table in);
i want to find the row of time, p1, p2 & s for a value of v that is close to certain number, example 35.4.
i want to find row of those data for each file.
2) the 2nd question is, i have a figure with plot of time, P2 for all the files (1 figure containing multiple plots)
how do i tell MATLAB to insert the values from question 1 for corresponding plots in the figure as legend.
Time P1 P2 V S
0.01 100 14.14214 29.02181 5.38719
0.02 110 14.8324 30.55477 5.527637
0.03 120 15.49193 32.01912 5.658544
0.04 130 16.12452 33.42332 5.781291
0.05 140 16.7332 34.77423 5.896968
0.06 150 17.32051 36.0775 6.006455
0.07 160 17.88854 37.33783 6.110469
0.08 170 18.43909 38.55921 6.209606
0.09 180 18.97367 39.74504 6.304367
0.1 190 19.49359 40.89826 6.395175

Accepted Answer

dpb
dpb on 25 Nov 2020
With the added information, I'd do something more like
Vtarget=35.4;
[files, path] = uigetfile ('.xlsx', 'MultiSelect', 'on');
if ischar(files), files=cellstr(files); end % handle one file is char string, not cell array
nFiles=numel(files);
hL=gobjects(nFiles,1); % preallocate graphics handles array
for i = 1:nFiles
fullfilename=fullfile(path,files(i)); % use fullfile to build; returns cellstr
tdata=readtable(fullfilename{:}); % and read the spreadsheet to table
ix=interp1(tdata.V,1:height(tdata),Vtarget,'nearest'); % find the nearest point for V
lgd=compose('Vtarget =%f.1',tdata.V(ix)); % build the display string for legend nearest V value
hL(i)=plot(tdata.Time, tdata.P2,'DisplayName',lgd); % plot the P2 variable; don't duplicate same data
if i==1, hold on, end % prepare add more to same figure
end
legend

More Answers (1)

dpb
dpb on 24 Nov 2020
t=array2table(data,'VariableNames',{'Time','P1','P2','V','S'});
Vtarget=35.4;
ix=interp1(t.V,1:height(t),35.4,'nearest')
ix =
5
>> t(ix,:)
ans =
1×5 table
Time P1 P2 V S
____ ___ ______ ______ _____
0.05 140 16.733 34.774 5.897
>>
To write a legend, use compose or num2str
legend(compose('Value for Vtarget =%d',Vtarget)
Salt to suit...
  6 Comments
dpb
dpb on 25 Nov 2020
doc readtable
...
See Also
...
Introduced in R2013b
readtable was introduced in R2013b so it should work just fine with R2016.
Gopal New
Gopal New on 25 Nov 2020
when using 'readtable', th e following error occurs
"Error using plot
Non-numeric data is not supported in 'Line'
Error in Test_2 (line 18)
plot (Time, P2)"
This doesn't happen with 'xlsread.'

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!