MATLAB Answers

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

1 view (last 30 days)
Gopal New
Gopal New on 24 Nov 2020
Commented: Gopal New on 25 Nov 2020
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


Sign in to comment.

Accepted Answer

dpb on 25 Nov 2020
With the added information, I'd do something more like
[files, path] = uigetfile ('.xlsx', 'MultiSelect', 'on');
if ischar(files), files=cellstr(files); end % handle one file is char string, not cell array
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

More Answers (1)

dpb on 24 Nov 2020
ix =
>> 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...


Show 3 older comments
dpb on 25 Nov 2020
"obiviously using 'xlsread' ..."
How is that "obvious"??? We can't see your terminal from here and you've posted zero lines of code.
xlsread is deprecated and not recommended; use readtable if you want a table.
The code I provided presumed your data were in an array; if not yet there, then you'll need to use whatever form you do have.
It's really not possible to provide specific answers without knowing what it is are dealing with which means we need to see the code itself (and an example of the spreadsheet never hurts).
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!