Too many input arguments when reading excel sheet into matlab plot

I am having trouble with plotting excel data in matlab.
My excel sheet is rather big, but I only want to plot the columns A(3:301), R(3:301) and Q(3:301). I get an "too many input arguments" error with the following code.
I dont really know how to tell matlab this, can somebody please help?
I get this error:
Warning: Column headers from the file were modified to make them valid
MATLAB identifiers before creating variable names for the table. The
original column headers are saved in the VariableDescriptions property.
Set 'PreserveVariableNames' to true to use the original column headers
as table variable names.
Error using tabular/plot
Too many input arguments.
Error in DataresultsPlot (line 27)
plot(time_data, T_proximal, current_data);
% Pfade zu den Ordnern mit den Excel-Dateien angeben
folder_paths = {
'C:\Users\anhky\Documents\Uni Stuff\Master\Masterarbeit\Evaluation\Data\d_2mm\Experiment 1',
};
% Loop über alle Ordner
for folder_index = 1:length(folder_paths)
folder_path = folder_paths{folder_index};
% Dateien in dem Ordner auflisten
file_list = dir(fullfile(folder_path, '*.xlsx'));
% Loop über alle Dateien in dem Ordner
for file_index = 1:length(file_list)
% Excel-Datei einlesen
file_path = fullfile(folder_path, file_list(file_index).name);
data = readtable(file_path);
% x- und y-Daten extrahieren
time_data = data(3:end, 1); % erste Spalte in Excel
T_proximal = data(3:end, 18); % zweite Spalte in Excel
current_data = data(3:end, 17); % dritte Spalte in Excel
% Graphen plotten
figure; % neues Figure-Fenster öffnen
plot(time_data, T_proximal, current_data);
% Achsenbeschriftungen hinzufügen
xlabel('time');
ylabel('T proximal');
zlabel('current');
% Titel hinzufügen
title(file_list(file_index).name);
% Graphen als PNG-Datei speichern
save_path = fullfile('C:\Pfad\zum\Speicherort', [file_list(file_index).name '.png']);
saveas(gcf, save_path);
end
end
Thanks!

답변 (2개)

Are you trying to create a 3D plot? You are passing 3 inputs (X,Y,Z?) into plot, but plot creates 2D plots.
If instead you want to plot the 3 columns with time being the X value, and the other 2 columns as your Y (so 2 separate dataseries), combine your Y-values into a single array using []. MATLAB treats each column as a unique data series.
% Make up data for this demo
time_data = linspace(0,6*pi)';
T_proximal = sin(time_data);
current_data = cos(2*time_data)/2;
plot(time_data, [T_proximal, current_data]);
data = readtable(file_path);
That returns a table() object
% x- und y-Daten extrahieren
time_data = data(3:end, 1); % erste Spalte in Excel
T_proximal = data(3:end, 18); % zweite Spalte in Excel
current_data = data(3:end, 17); % dritte Spalte in Excel
When you use () indexing on a table object, you get out table objects, so time_data and T_proximal and current_data here are all table() objects, each of which has a single variable.
plot(time_data, T_proximal, current_data);
with time_data being a table() object, you are invoking tabular plot rather than regular numeric plot.
plot(TBL,XVAR,YVAR) plots the variables xvar and yvar from the table
tbl. To plot one data set, specify one variable for xvar and one
variable for yvar. To plot multiple data sets, specify multiple
variables for xvar, yvar, or both. If both arguments specify multiple
variables, they must specify the same number of variables
plot(TBL,YVAR) plots the specified variable from the table against the
row indices in the table. If the table is a timetable, the specified
variable is plotted against the row times from the timetable.
You are using the three-input version of plot() so you would have to be passing in a table and indicators for two variables. The indicators must be string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype()
So for example you could
plot(data(3:end), [1], [18 17])

댓글 수: 9

thanks! INow I get this error:
Error using DataresultsPlot (line 27)
Subscripting into a table using one subscript (as in t(i)) or three or
more subscripts (as in t(i,j,k)) is not supported. Always specify a row
subscript and a variable subscript, as in t(rows,vars).
I am kinda not familiar mit matlab, so I dont really understand this... appreciate any help!
hmm still not working, i get "too many input parameters" error.
If I do plot(data(3:end, :), [1], [18 17]), I dont nee this part any more:
time_data = data(:, 1); % erste Spalte in Excel
T_proximal = data(:, 2); % zweite Spalte in Excel
current_data = data(:, 3); % dritte Spalte in Excel
right?
You are correct that those lines would not be needed.
You need to decide whether you want to pass a table in as the first parameter to plot, or if you want to pass in numeric values. For the numeric values choice:
time_data = data{3:end, 1}; % erste Spalte in Excel
T_proximal = data{3:end, 18}; % zweite Spalte in Excel
current_data = data{3:end, 17}; % dritte Spalte in Excel
plot(time_data, [T_proximal, current_data]);
thanks for the help!
Its saying now that theres
Error using plot
Size mismatch in Param Cell / Value Cell pair
But I checked and they do have the same size.. do you maybe know what else can cause this error?
Try
time_data = data{3:end, 1}; % erste Spalte in Excel
T_proximal = data{3:end, 18}; % zweite Spalte in Excel
current_data = data{3:end, 17}; % dritte Spalte in Excel
plot(time_data, [T_proximal(:), current_data(:)]);
which should not make a difference, but...
On the other hand, I have to question why you are extracting rows 3 to end instead of using all of the columns. That suggests to me that you probably had headers that you do not want to plot. But in the case where readtable() does not properly guess about which lines are headers and which are not, then it is common for readtable() to decide that a column is text instead of numeric. In such a case, time_data could have ended up as containing text instead of numeric values, and plot() might think you are trying to pass in options.
If this is what is happening, then you should be using something like
data = readtable(file_path, 'HeaderLines', 2, 'ReadVariableNames', false);
or possibly you might need 3 header lines in your situation.
yes, it didnt make a difference unfortunately.
and yes, the first 3 rows are just headlines so I thought I have to cut them out. I integrated your comment, but it saying now that
Invalid parameter name: HeaderLines.
Maybe I should not use readtable at all? In fact, I just want to plot the data from my excel, I dont really know how to proceed.. sorry Im really a newbie
I used xlsread and it works!
Which MATLAB version are you using? And can you attach a sample data file?

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

카테고리

질문:

2023년 3월 13일

댓글:

2023년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by