Finding the maximal value in a matrix, within a specific column, and show the value in the output.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sergio
el 14 de Feb. de 2024
Comentada: Torsten
el 14 de Feb. de 2024
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 comentario
Respuesta aceptada
Voss
el 14 de Feb. de 2024
Editada: Voss
el 14 de Feb. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!