Create plot from large data in excel

10 Ansichten (letzte 30 Tage)
chlor thanks
chlor thanks am 8 Jul. 2016
Kommentiert: Star Strider am 11 Jul. 2016
I have a very large excel spreadsheet that needs to be coded and plotted in matlab, so before I am able to attempt that, I want to make sure I know how to plot a simple example first. I made the following simple excel file named "run.xlsx" with a simple spreadsheet and hoping to replicate the same graph in matlab...
As I mentioned, since I will be plotting very large and variant spreadsheets with one "universal" code, I want to use loop instead of real numbers when attempting so. This is what I have tried:
[data,text,raw] = xlsread('run.xlsx');
[r,c]=size(raw);
if ~isempty(text(:,1))
for k1=1:r
leg=text(k1,1))
end
end
if ~isempty(text(1,:))
for k2=1:c
ax=text(1,k2)
end
end
p=struct(num2cell(data,1));
plot(p);
legend(leg)
xlabel(ax)
I have never done anything like this before and the code is poorly written, I hope someone can help me point out where I did wrong and how I can better my little script.
Thank you!

Akzeptierte Antwort

Star Strider
Star Strider am 8 Jul. 2016
This should get you started:
[data,text,raw] = xlsread('chlor thanks run.xlsx');
lgnd = text(2:3,1); % Get Text For LEgend
xtlbl = text(1,2:end); % Get Text For X-Labels
x = 1:size(data,2); % Define ‘x’ Vector
figure(1)
plot(x, data, 's-')
grid
legend(lgnd, 'Location','SE')
set(gca, 'XTick', x, 'XTickLabel',xtlbl)
axis([0 5 0 9])
  2 Kommentare
chlor thanks
chlor thanks am 11 Jul. 2016
Thank you Star! This is the simplest solution I have seen so far, it will help me getting started.
Star Strider
Star Strider am 11 Jul. 2016
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 8 Jul. 2016
In your
leg=text(k1,1))
you overwrite the variable leg each time. You should use something like
leg{k1} = text{k1,1};
However, you also need to be careful because the second output of xlsread(), the text output, will be trimmed to hold only the text objects, and so will not necessarily be the same size as raw, but you are looping over the number of rows in raw. You would be safer either iterating over rows of text or extracting data from raw . In your sample it does not make a difference but as this is a learning exercise best to learn this aspect. Another thing to watch out for is that you start the looping on row 1 but row 1 is going to be the column headers. And you can also optimize. With no loop at all you can use
leg = text(2:end,1);
ax = text(1,2:end);
However, I would not recommend using text as a variable name at all, as it is the name of the MATLAB text() graphic primitive. I had in fact written up a paragraph about how you should not be using the text object handles as inputs to legend() before I noticed that you had used the unfortunate variable name. Even if you understand it, please have pity on the other people who are going to read your code and think that text is the MATLAB call by that name.
I would also not recommend using ax as a variable name to store text values. People reading the code are probably going to expect axes handles in a variable name named ax
Okay, now down to
p=struct(num2cell(data,1));
plot(p);
The output of num2cell() is going to be a cell array in which each element is numeric. That is not acceptable input to struct(), which requires a string as its first parameter (or an Object, in some cases.) You should probably just be using
plot(data)
  1 Kommentar
chlor thanks
chlor thanks am 11 Jul. 2016
Bearbeitet: chlor thanks am 11 Jul. 2016
Walter,thank you very much for your help, I'll keep those tips in mind!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by