ploting Multiple graphs from multiple excel sheets using Matlab 2018

조회 수: 23 (최근 30일)
Najwa Abdel Latif
Najwa Abdel Latif 2021년 8월 26일
댓글: Walter Roberson 2021년 8월 26일
[status,sheets,xlFormat]=xlsfinfo('Majorfaults_300m_10profiles.xlsx')
A=cell2mat(sheets)
for idx_cell = 1:size(sheets,1) out.(sheets{idx_cell})={}; end
A = cell2mat(sheets(1))
A= convertCharsToStrings(cell2mat(sheets(1)))
This is what I have tried so far, but my sheets are not being converted into matrices! I need to do that and then from each sheet plot a graph with column A vs. Column B E F D ..
and repeat for all sheets I have.
At the end I need 68 Figures from 68 sheets.
Thank you!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 8월 26일
filename = 'Majorfaults_300m_10profiles.xlsx';
[status, sheets] = xlsfinfo(filename);
if isempty(status); error('Cannot find sheets in file "%s"', filename); end
out = struct();
for idx_cell = 1:size(sheets,1)
thissheet = sheets{idx_cell};
thisdata = readmatrix(filename, 'sheet', thissheet);
out.(thissheet) = thisdata;
fig = figure();
ax = axes(fig);
plot(ax, thisdata(:,1), thisdata(:,[2 5 6 4]));
legend(ax, {'B', 'E', 'F', 'D'});
xlabel(ax, 'A');
title(ax, thissheet);
end
This code reads and plots, and also stores the data in the struct out .
One figure is created for each sheet.
... That's a lot of figures. Wouldn't you prefer to write the result as images or something like that? Maybe a movie?
  댓글 수: 2
Najwa Abdel Latif
Najwa Abdel Latif 2021년 8월 26일
Thank you so Much for you reply.
The If condition in the code returns error
... if isempty(status); error('Cannot find sheets in file "%s"', filename); end ...
and if I exclude it from the code and run it i recieve the following error msg
Error in internet (line 10)
thissheet = sheets{idx_cell};
I am not sure where the problem is.. mean while I have developed a code where I got the data from my excel file and was able to plot it.
please see below,
...........................................................
%%
[status,sheets,xlFormat]=xlsfinfo('Majorfaults_300m_10profiles.xlsx')
for k=1:numel(sheets)
data{k}=xlsread('Majorfaults_300m_10profiles.xlsx',sheets{k});
end
for i = 2:6
plot( data{1}(:,1),data{1}(:,i))
hold on
end
%%
...........................................................
this code worked.. I just can't make a nother loop that plots all at once. in my code I need to write the plotting comand 68 times..
Walter Roberson
Walter Roberson 2021년 8월 26일
These days replace the xlsinfo call with https://www.mathworks.com/help/matlab/ref/sheetnames.html
sheets = sheetnames(filename);
and remove the isempty(status) test.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by