Finding the maximal value in a matrix, within a specific column, and show the value in the output.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, I have a matrix that I need to extract the maximal value of column D, which is the 4th column.
I use the following command
D=readtable('tempDataTrollhFlygpl.xlsx')
[~,x] = max(D(:,4));
B = D(x,:);
However, it does not work, and I get the error
Error using tabular/max
Applying the function 'max' to the variable 'Latitud_decimalgrader_' generated an error.
Error in untitled (line 2)
[~,x] = max(D(:,4));
Caused by:
Error using max
Invalid data type. First argument must be numeric or logical.
I should specify the rows after row 11 I think, but how that done?
I tried islocalmax instead, with plot command, but also this case didn't work.
A=readtable('tempDataTrollhFlygpl.xlsx')
plot(A(:,3:end), A(:,4:end))
xlabel('Temperature')
ylabel('Time')
TF1 = islocalmax(A,'FlatSelection','first');
Thanks
1 Kommentar
Akzeptierte Antwort
Voss
am 14 Feb. 2024
Bearbeitet: Voss
am 14 Feb. 2024
D(:,4) is a table (consisting of one column - the 4th column of D), not a numeric array; that's what the error is trying to tell you.
To get the data out of the table, you can use the syntax D{:,4}. However, in this case, column 4 of your table is a cell array of character vectors, not a numeric array, so you'll get a similar error using that syntax. (Note the single quotes around the numbers in column 4 below, indicating character vectors.)
D=readtable('tempDataTrollhFlygpl.xlsx')
"I should specify the rows after row 11 I think, but how that done?"
Specify 'NumHeaderLines', 10 in your readtable call:
D=readtable('tempDataTrollhFlygpl.xlsx', 'NumHeaderLines', 10) % specifying 10 header lines
Now, you need to convert those character vectors to numbers, either by setting the appropriate import options before readtable is called, or after the fact by converting e.g. using str2double
val = str2double(D{:,4})
[~,x] = max(val)
B = D(x,:)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!